Hi guy,
I have this small vba code to copy a picture from an excel file into powerpoint. it seems to work with excel 2010 but not with excel 2013.
can somebody help understand why.
Dim Ppres As PowerPoint.Presentation
Dim Papp As PowerPoint.Application
Papp.Visible = msoTrue
Set Pslide = Ppres.Slides.Add(1, ppLayoutBlank)
Sheet1.Range("A1:I17").CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Dim Pshape As PowerPoint.Shape
'(the line above has to be comment out in order to avoid run time error after setup)
Set Pshape = Pslide.Shapes.Paste
Pshape.Left = 100
Pshape.Top = 50
Pshape.ScaleHeight 1.2, msoTrue
Hi Babacar,
The variable Pshape was not defined. Just add this line to your declarations
Dim Pshape As Object
Regards
Phil
Hi Phil,
thanks for taking the time to reply for my request. i did initially defined as Dim Pshape As Shape and it did not work. then i used us you suggested Dim Pshape As Object but still doesn't work. i wonder if its related to excel 2013. Can you try again please
Hi Babacar,
I'm using Excel 2013 and it works.
Have you tried stepping through the code using F8 to see where the code breaks?
Please attach your workbook and I will see what I can find.
Also, please specify exactly what is not working.
- What line of code does the debugger highlight?
- What error message are you receiving?
Regards
Phil
Hi Phil,
the error message is [run time error 424 - object required].
the line code where it stop is : Pshape.Left = 100
below are the codes:
Dim Ppres As PowerPoint.Presentation
Dim Papp As PowerPoint.Application
Papp.Visible = msoTrue
Set Pslide = Ppres.Slides.Add(1, ppLayoutBlank)
Sheet1.Range("A1:I17").CopyPicture Appearance:=xlScreen, Format:=xlPicture
Dim Pshape As Object
'(the line above has to be comment out in order to avoid run time error after setup)
' Set Pshape = Pslide.Shapes.Paste
Set Pshape = Pslide.Shapes.PasteSpecial(DataType:=2)
Pshape.Left = 100
Pshape.Top = 50
Pshape.ScaleHeight 1.2, msoTrue
[Image Can Not Be Found]
Hi Babacar,
The code above and the workbook you emailed me both work on my Excel 2013.
If your code is stopping at Pshape.Left = 100 with the error [run time error 424 - object required] that indicates that Pshape is not defined. But if you pasted the picture into PP then Pshape should be defined.
Does the picture appear in PP?
The only way I can recreate this error is to comment out the DIM and SET statements for Pshape i.e.:
Sub OpenPowerPoint() Dim Pslide As PowerPoint.Slide Dim Ppres As PowerPoint.Presentation Dim Papp As PowerPoint.Application Set Papp = New PowerPoint.Application Papp.Visible = msoTrue Set Ppres = Papp.Presentations.Add Set Pslide = Ppres.Slides.Add(1, ppLayoutBlank) 'Range("A1:I17") Sheet1.Range("A1:I17").CopyPicture Appearance:=xlScreen, Format:=xlPicture 'Dim Pshape As Object '(the line above has to be comment out in order to avoid run time error after setup) ' Set Pshape = Pslide.Shapes.Paste 'Set Pshape = Pslide.Shapes.PasteSpecial(DataType:=2) Pshape.Left = 100 Pshape.Top = 50 Pshape.ScaleHeight 1.2, msoTrue End Sub
Can you please add this to the top of all your code modules too, it checks for undefined variables
Option Explicit