gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:29:25 26 4
gpt4 key购买 nike

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

我也得到

runtime error -2147188160 - 'Shapes(unknown member) : Invalid request



ppslide.Shapes.PasteSpecial ppPasteEnhancedMetafile执行。这在执行期间随机发生。

这就是我想要做的: exec_sorted 是一个集合的字典
For Each iter1 In accExec_sorted.Keys()

Set ppapp = New PowerPoint.Application
Set pppress = ppapp.Presentations.Add
pppress.PageSetup.SlideSize = ppSlideSizeLetterPaper
Set ppslide = pppress.Slides.Add(1, ppLayoutTitle)
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, ppLayoutBlank)


ind_len.ChartObjects("Chart 6").Select
Selection.Copy
ppslide.Shapes.PasteSpecial ppPasteEnhancedMetafile
Set myshape = ppslide.Shapes(1)
myshape.LockAspectRatio = False

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

ind_len.ChartObjects("Chart 7").Select
Selection.Copy
ppslide.Shapes.PasteSpecial ppPasteEnhancedMetafile
Set myshape = ppslide.Shapes(2)
myshape.LockAspectRatio = False

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


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

Next iter1

最佳答案

好的 Shreyans,我不得不修改代码才能最终工作并玩弄一些东西,但它确实有效。然后,我将您的代码插入到我拥有的代码中。问题是您试图将图表对象作为对象粘贴到演示文稿中,而不是将对象复制为图片。一旦我这样做并将对象设置为空,它就起作用了。这是我的代码。

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

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

关于excel - VBA 运行时错误 -2147188720 对象不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54508873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com