如何查找Powerpoint形状的索引

时间:2018-02-24 05:16:00

标签: vba powerpoint

我正在编写VBA脚本,将Excel工作表中的图表复制+粘贴到现有的Powerpoint演示文稿中作为图片。

到目前为止,我成功地将复制粘贴作为图片,但未能更改粘贴图片的大小。我相信粘贴的图片是一个形状,所以我尝试做类似的事情:

 For Each iChart In arrChart
        xlWorkBook.Worksheets("PPT").ChartObjects(iChart).CopyPicture
        ActivePresentation.Slides(iCurrSlide).Shapes.Paste
        'Reshape
        ActivePresentation.Slides(iCurrSlide).Select
            With ActivePresentation.Slides(iCurrSlide).Shapes.Item(5)
                .Select
                .Height = ActiveWindow.Presentation.PageSetup.SlideHeight
                .Width = ActiveWindow.Presentation.PageSetup.SlideWidth
                .Left = 0
                .Top = 0
            End With
        iCurrSlide = iCurrSlide + 1
Next iChart

但是我似乎得到了错误的索引,所以我想知道是否有办法捕获图片的索引(作为一个形状)?由于我无法为图片命名,所以我的双手被绑住(如果我将每张图片命名为粘贴,我宁愿将脚本放入垃圾箱并简单地手动粘贴),我无法改变pptx的任何内容和xlsx。

1 个答案:

答案 0 :(得分:0)

避免选择任何东西,除非绝对必要,但很少这样做。 假设这是在PPT内的VBA中发生的:

Dim oSh as Shape

For Each iChart In arrChart
        xlWorkBook.Worksheets("PPT").ChartObjects(iChart).CopyPicture

        ' Set a reference to the just-pasted shape
        Set oSh = ActivePresentation.Slides(iCurrSlide).Shapes.Paste(1)

        'Reshape

        ' You don't need this:
        ' ActivePresentation.Slides(iCurrSlide).Select
            ' With ActivePresentation.Slides(iCurrSlide).Shapes.Item(5)
            With oSh
                ' No need to select it as long as you've got a reference to it:
                '.Select

                ' I'd use ActivePresentation.PageSetup here but it may not matter
                .Height = ActiveWindow.Presentation.PageSetup.SlideHeight
                .Width = ActiveWindow.Presentation.PageSetup.SlideWidth
                .Left = 0
                .Top = 0
            End With    ' oSh
        iCurrSlide = iCurrSlide + 1
Next iChart