无法在Office 2011上添加简单形状

时间:2013-11-23 01:46:37

标签: macos vba excel-vba powerpoint-vba excel

我试图在OSX office 2011上使用VBA自动制作一些幻灯片。但是,我无法添加基本形状。我认为这可能是项目复杂性的问题,所以我把它归结为附加的测试脚本。当我运行它时,我从最后一行得到“无效的过程或方法调用”错误。所有这些都可以在PC上运行,当我从PowerPoint运行它时可以工作,但是当我尝试从excel运行它时,对象模型似乎变得更加复杂。任何帮助将不胜感激!

Sub test()

Dim ppApp As PowerPoint.Application
Dim pptPres As Presentation

Dim powerpointApp As PowerPoint.Application
Dim powerpointPres As Presentation

Set powerpointApp = New PowerPoint.Application

powerpointApp.Visible = msoTrue
Application.ScreenUpdating = True
Set powerpointPres = powerpointApp.Presentations.Add

Dim sld1 As Slide
Dim oLayout As CustomLayout
Set oLayout = powerpointPres.Designs(1).SlideMaster.CustomLayouts(1)
Set sld1 = powerpointPres.Slides.addSlide(1, oLayout)

Dim shp1 As SmartArt

Dim shapes1 As Object

Set shp1 = sld1.Shapes.addShape(msoShape10pointStar, 1, 1, 1, 1)



End Sub

1 个答案:

答案 0 :(得分:0)

每当您有疑问时,如果它是一个对象,则将其声明为Object,如果它是变量,则将其声明为Variant,然后只需添加一个监视即可查看{该对象/变量的{1}}。

例如

将此代码粘贴到模块中并添加Type

Watch

enter image description here

对象的另一个例子

enter image description here

同样适用于变量

enter image description here

所以在第一种情况下,该对象应该被声明为Option Explicit Sub Sample() Dim shp As Object Set shp = ActivePresentation.Slides(1).Shapes.AddShape(msoShape10pointStar, 10, 10, 10, 10) Debug.Print shp.Name End Sub ,而在第二种情况下,对象应该分别被声明为ShapeCustomLayout,最后是第三,变量必须是Slide