VBA运行时错误-2147188720对象不存在

时间:2019-02-04 00:14:42

标签: excel vba powerpoint powerpoint-vba

我正在尝试通过excel运行VBA代码,该代码使用循环创建多个powerpoint文件。但是,每次循环递增时,都会出现运行时错误。当configure.ac在第一个迭代之后执行任何迭代时,就会出现错误。

我也得到

  

运行时错误-2147188160-'形状(未知成员):无效的请求

执行myshape.LockAspectRatio = False时。这是在执行过程中随机发生的。

这就是我想要做的:exec_sorted是集合的字典

ppslide.Shapes.PasteSpecial ppPasteEnhancedMetafile

1 个答案:

答案 0 :(得分:1)

好的Shreyans,我必须修改代码才能在我的终端上正常工作并尝试一些操作,但是它可以正常工作。然后,我将您的代码插入到我已有的代码中。问题是您试图将图表对象作为对象粘贴到演示文稿中,而不是将对象复制为图片。一旦完成该操作,然后将对象设置为无效即可。这是我的代码。

注意:您可以将其修改回使用Powerpoint.Application变量,而无需使用create对象。我只是这样做是为了让我更轻松。

Sub CopyPastePicture()

    For Each iter1 In accExec_sorted.Keys()

        Set ppapp = CreateObject("PowerPoint.Application")
        Set pppress = ppapp.Presentations.Add
        pppress.PageSetup.SlideSize = 2
        Set ppslide = pppress.Slides.Add(1, 1)
        ppslide.Shapes(1).TextFrame.TextRange = iter1

        i = 2
        Set lenderID = accExec_sorted(iter1)

        For Each iter In lenderID
            ind_len.Range("l_id1") = iter

            Set ppslide = pppress.Slides.Add(i, 12)

            ind_len.ChartObjects("Chart 6").CopyPicture xlPrinter, xlPicture
            ppslide.Shapes.PasteSpecial 2
            Set myshape = ppslide.Shapes(ppslide.Shapes.Count)

            myshape.LockAspectRatio = False

            myshape.Left = 420
            myshape.Top = 40
            myshape.Width = 290
            myshape.Height = 160

            Set myshape = Nothing

            ind_len.ChartObjects("Chart 7").CopyPicture xlPrinter, xlPicture
            ppslide.Shapes.PasteSpecial 2
            Set myshape = ppslide.Shapes(ppslide.Shapes.Count)

            myshape.LockAspectRatio = False

            myshape.Left = 420
            myshape.Top = 205
            myshape.Width = 290
            myshape.Height = 160

            Set myshape = Nothing
            Set ppslide = Nothing

            i = i + 1

        Next iter

        pppress.SaveAs intro.Range("dest_path") & intro.Range("investor") & "_" & intro.Range("period") & "_" & iter1 & ".pptx"
        pppress.Close
        ppapp.Quit
        Set ppapp = Nothing
        Set pppress = Nothing
        Set ppslide = Nothing
        Set myshape = Nothing

    Next iter1

End Sub