将图片添加到以编程方式创建的PowerPoint演示文稿 [英] Adding a Picture to a programmatically created PowerPoint presentation
问题描述
大家好,
我正在尝试运行一个简单的Word宏来创建一个新的PowerPoint演示文稿,只需从预定义的路径向演示文稿中添加一张图片。代码或多或少有效,因为正确打开了演示文件文件,并且图片被添加到新创建的幻灯片
但不知何故我总是得到运行时错误'13'类型不匹配。
I'm trying to run a simple Word macro that creates a new PowerPoint presentation and simply adds a picture to the presentation from a predefined path. The code works more or less because the presentation file is opened correctly and the picture is added to the newly created slide but somehow I always get a runtime error '13' Type mismatch.
如果我编写一个用于PowerPoint文件的宏来将图片添加到现有演示文稿中,它可以正常工作。
If I write a macro that is used in a PowerPoint File to ad a picture to an existing presentation it works fine.
这是我在Word中使用的宏的代码:
Here is the code for the macro I use in Word:
Option Explicit
Sub TestMakro()
'
' TestMakro Macro
'
'
Dim oSld As Slide
Dim oPic As Shape
Dim testInlineShape As InlineShape
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPres = pptApp.Presentations.Add(msoTrue)
Set oSld = pptPres.Slides.Add(pptPres.Slides.Count + 1, ppLayoutBlank)
Set oPic = oSld.Shapes.AddPicture("C:\Temp\data_files\image001.png", msoFalse, msoTrue, 100, 100)
End Sub
感谢您的帮助。
Jan
推荐答案
你好
尝试使用
Dim oPic As PowerPoint.Shape
如果未明确指定"Word.Shape"用作类型。作为PPT"AddPicture",方法返回一个PowerPoint形状,这将导致类型不匹配。
If not specified explicitly "Word.Shape" is used as type. As the PPT "AddPicture" method returns a PowerPoint shape this will lead to a type mismatch.
你对"oSld"的Dim语句没有任何问题。幻灯片,因为Word中不存在幻灯片对象。
You have no issue with the Dim statement for the "oSld" slide because no slide object exists in Word.
干杯,卢卡
这篇关于将图片添加到以编程方式创建的PowerPoint演示文稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!