简体   繁体   中英

"This view does not support selection" error on VBA Excel when copy-pasting and resizing charts on Powerpoint slides

I am working on a project to automate powerpoint presentations. For this, I create charts on Excel and then I use VBA codes to copy and paste them on an existing powerpoint template.

However, when I try to resize and position my charts, I encounter the following problem: "Selection (unknown member) : Invalid request. This view does not support selection."

Here is my code :

Sub powerpoint()

            Dim Powerpointapp As PowerPoint.Application
            Dim PowerPointPrsn As PowerPoint.Presentation
            Dim cht As ChartObject
            Dim strpath As String
            Dim rng As Range
            Dim mySlide As Object
            Dim myShape As Object

    ' paths to wkb and template
            strpath = ThisWorkbook.Path & "\template.pptm"
        
    ' create new ppt with template
            Set Powerpointapp = New PowerPoint.Application
            Set PowerPointPrsn = Powerpointapp.Presentations.Open(strpath)
            
     ' copy paste chart 1 to slide 1 and resizing 
        Set cht = Worksheets("sheet name").ChartObjects(1)
        cht.Copy
        DoEvents
        PowerPointPrsn.Slides(1).Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
        Powerpointapp.ActiveWindow.Selection.ShapeRange.Left = 50
        Powerpointapp.ActiveWindow.Selection.ShapeRange.Top = 120
        Powerpointapp.ActiveWindow.Selection.ShapeRange.Width = 590

My issue is starting at the line

Powerpointapp.ActiveWindow.Selection.ShapeRange.Left = 50

Thank you for your help !

As p77u77n77k mentioned, you haven't selected anything, so referring to the .Selection object will throw an error. You could instead:

At the top of the module, DIM oSh as Shape

Then

Set oSh = PowerPointPrsn.Slides(1).Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)

With oSh
   .Left = 50
   .Top = 120
   .Width = 590
End with

Note that I've used ppPasteEnhancedMetafile rather than ppPasteMetafilePicture. I'm not sure what the diff is, but it seems to work better here. Microsoft's docs on this are all but useless. I suspect ppPasteMetafilePicture will give you a metafile rendition of whatever picture (ie raster image) is on the clipboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM