vba 错误 - 无效请求。要选择一个形状,它的视图必须处于活动状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22748853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:34:48  来源:igfitidea点击:

Error - Invalid request. To select a shape its view must be active

vbaexcel-vbapowerpointexcel

提问by Puneet

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:

我是 VBA 的新手。我正在编写代码以将粘贴图表从 excel 复制到 powerpoint 演示文稿的不同幻灯片。我可以完美对齐地将图表粘贴到 ppt 的幻灯片 1 上;但是对于第二张图表,我收到一条错误消息:“请求无效。要选择形状,其视图必须处于活动状态。”。请求所有人支持我更正以下代码:

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

回答by user1

Use

ActiveWindow.View.GotoSlide oSlide.SlideIndex

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

在选择幻灯片上的形状之前激活视图:

More information can be found here

更多信息可以在这里找到