2

I've created a code that copies pictures from Excel to a new PowerPoint presentation. The code works fine for MS Office 2016, but not MS Office 2010. Particularly, a picture that is exported to PowerPoint will not be resized in .pptx for 2010.

How can I fix this?

Here is the problematic piece of code that does not work in MS 2010:

    Application.Goto Reference:="Full_Account_Performance"
    Application.CutCopyMode = False
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

    PPPres.Slides(x).Shapes.PasteSpecial

    On Error Resume Next                
    With PPApp.ActiveWindow.Selection.ShapeRange 
        .ScaleHeight 0.435, msoFalse, msoScaleFromTopLeft 
        'Powerpoint 2010 ingnors it... but in 2016 it is fine
        .Left = 10
        .Top = 55
    End With
Community
  • 1
  • 1

2 Answers2

2

In PowerPoint 2010, it sometimes skips the lines after you paste a picture using Shapes.PasteSpecial command (they are not skipped, just the code runs them before it completes to paste the Picture).

There is a work-around, you can add a delay of a second, and the code will work (the lines below won't be skipped).

The code below will set an Object to the pasted picture in PowerPoint, and later just modify myShape properties.

Note: The code below uses Late Binding, but it will work also for Early Binding.

Code

Dim PPPres                              As Object
Dim PPSlide                             As Object
Dim myShape                             As Object

' set the slide object - x is the slide number
Set PPSlide = PPPres.Slides(x)  

' Set an Object to the Pasted PowerPoint picture
Set myShape = PPSlide.Shapes.PasteSpecial(0, msoFalse) ' ppPasteDefault = 0
With myShape
    ' it skips the lines below, add a delay
    Application.Wait Now + TimeValue("00:00:01")

    .ScaleHeight 0.435, msoFalse, msoScaleFromTopLeft
    .Left = 10
    .Top = 55
End With
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
0

This appears to be the issue:

With PPApp.ActiveWindow.Selection.ShapeRange 

Try:

With PPPres.Slides(x).Shapes(y)

where y = the picture you've just pasted. Since you're not setting a reference to it, you may need to iterate through the shapes in the slide to find which shape it is.

Winterknell
  • 585
  • 3
  • 6
  • hi, that worked but not for all pictures that i exported BUT i must say i moved on a bit! in total i need to export 22 pics. i pic one per slide. i set y = 1 and in the loop i also did a simple calculus y=y+1 BUT only the second picture was resized... this is very strange as all pics are exported with no problems to particular slides, so a cycle that i have ->works, but it resizes only second slide... Any idea why? when i put X instead of Y does not work for all neither... – Marek Korenko Mar 11 '17 at 18:46
  • I think Shai Rado nailed it. If their solution works reliably don't forget to give their answer a tick. Either way, glad to hear. – Winterknell Mar 11 '17 at 22:43