6

I am new to VBA. I am writing code to copy paste charts from excel to different slides of a powerpoint presentation. I am able to paste chart on slide 1 of a ppt in perfect alignment; but for the 2nd chart I am getting an error saying: "Invalid request. To select a shape its view must be active.". Would request all to support me on correcting this code below:

Sub ExcelAuto()
Dim PPT As PowerPoint.Application
Dim PPTFile As PowerPoint.Presentation
Dim ActiveSlide As PowerPoint.Slide

Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open fileName:="Path"

Set PPTFile = PPT.ActivePresentation
PPT.ActiveWindow.ViewType = ppViewSlide

        ActiveWorkbook.Sheets("Charts").ChartObjects("Chart 6").CopyPicture
        With PPTFile.Slides(1)
        .Shapes.Paste.Select
         PPT.ActiveWindow.Selection.ShapeRange.Left = 37
         PPT.ActiveWindow.Selection.ShapeRange.Top = 127
        End With

        ActiveWorkbook.Sheets("Charts").ChartObjects("Chart 8").CopyPicture
        With PPTFile.Slides(2)
        .Shapes.Paste.Select
         PPT.ActiveWindow.Selection.ShapeRange.Left = 37
         PPT.ActiveWindow.Selection.ShapeRange.Top = 354
        End With

    Set PPT = Nothing
    Set PPTFile = Nothing
    Set ActiveSlide = Nothing


End Sub
4
  • it's because your second slide is inactive. You can't select shape on inactive slide Mar 30, 2014 at 19:42
  • 1
    In ppt 2010+ the Paste method returns a ShapeRange object representing the pasted shape - you can use that reference to work with it instead of relying on Selection. Mar 30, 2014 at 20:02
  • Hello all, thank you for your comments..I am writing this code in MS Office 2007 and can you help in activating second slide..i use "activeslide" but this gives me an error..Thank again.. :-)
    – Puneet
    Apr 1, 2014 at 18:51
  • 1
    Thank You so much guys; I cracked this..Only addition to the code was to Dim SlideNum As Integer and defining each slide # (example: SlideNum = 1 PPTFile.Slides(SlideNum).Select.. Cheers..!! :-)
    – Puneet
    Apr 2, 2014 at 5:13

1 Answer 1

9

Use

ActiveWindow.View.GotoSlide oSlide.SlideIndex

to activate the view before you select the shape on the slide:

More information can be found here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.