运行时错误'-2147188160(80048240)':应用程序(未知成员):无效的请求.子或功能未定义 [英] Run time error '-2147188160 (80048240)': Application (unknown member) : Invalid request. sub or function not defined
问题描述
尝试运行以下宏时出现此错误.该错误在第一次执行"AddSlide"(!)时才显示.宏继续(在消息框中单击结束"后),并且运行良好,之后没有任何错误消息.
I am getting this error while trying to run the following macro. The error shows up on the first(!) time the "AddSlide" is executed and only then(!). The macro continues (after I click on "End" in the message box) and works well without any error message after that.
Sub PushChartsToPPT_1()
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptShp As PowerPoint.Shape
Dim EndTime As Single
Dim rng As Range
Dim cht As Chart
Dim ws As Worksheet
Dim i, j As Single
Dim MainWorkBook, tempWorkBook As Workbook
Dim tempSheet As Worksheet
Dim pptCL As CustomLayout
Dim myShape As Object
Dim DestinationPPT, str As String
Set MainWorkBook = ActiveWorkbook
'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
DestinationPPT = "Template.pptx"
Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only
'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL
ppt.Visible = msoTrue
For Each ws In MainWorkBook.Worksheets
For i = 1 To ws.ChartObjects.Count
'>>>>> error next line
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL) '>>>>>error on this line
pptSld.Select
Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
DoEvents
pptSld.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
Next i
Next ws
End Sub
推荐答案
上一个循环是否有可能结束,而不是在标题和内容"幻灯片上执行退出"?这将意味着值pptCL是未定义的.但是,那将是错误的错误报告;使用pptCL undefined运行的那一行会导致无效的过程调用或参数".
Is it possible that the previous loop is finishing, instead of doing an Exit For at a 'title and content' slide? That would mean that the value pptCL is undefined. However, that would be the wrong error report; that line run with pptCL undefined causes an "invalid procedure call or argument".
但是奇怪的是,您的代码在打开powerpoint文件的那一行上对我来说就死了:
But the odd thing is that your code died for me right on the line where the powerpoint file is opened, the line:
设置pptPres = ppt.Presentations.Open(DestinationPPT,True)'只读
Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only
...它死于那个确切的自动化错误".我将代码与可以在其中打开PowerPoint文件的代码进行了比较,并且该代码中包含以下行:
...and it died with that exact "automation error". I compared to code I have where a powerpoint file opened fine, and that code had the line:
ppt.Visible = msoTrue
ppt.Visible = msoTrue
...使Powerpoint应用程序可见,就在打开"命令上方.当我将该行添加到您的代码中时,在运行过程中错误消失了.
...making the powerpoint application visible, just above the "open" command. When I added that line to your code, the error went away, in my run.
这可能是完全的红鲱鱼,但是由于它对我有用,并且由于您没有其他任何答案,请花点时间尝试一下.
This may be a complete red herring, but since it worked for me, and since you aren't getting any other answers, take a moment and try it.
这篇关于运行时错误'-2147188160(80048240)':应用程序(未知成员):无效的请求.子或功能未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!