Forum

Copying Excel chart...
 
Notifications
Clear all

Copying Excel chart into powerpoint slide

6 Posts
2 Users
0 Reactions
88 Views
(@bgueye)
Posts: 4
Active Member
Topic starter
 

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.

 

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 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
   
 
End Sub
 
Posted : 01/04/2017 12:23 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Babacar,

The variable Pshape was not defined.  Just add this line to your declarations

Dim Pshape As Object

Regards

Phil

 
Posted : 04/04/2017 10:27 pm
(@bgueye)
Posts: 4
Active Member
Topic starter
 

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

 
Posted : 05/04/2017 8:47 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

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

 
Posted : 05/04/2017 8:54 pm
(@bgueye)
Posts: 4
Active Member
Topic starter
 

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:

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

[Image Can Not Be Found]

 
Posted : 06/04/2017 9:35 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

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

 
Posted : 07/04/2017 12:18 am
Share: