将用户窗体添加到 Power Point 文件。
在该UserForm上放置一个CommandButton和一个ListBox。
将以下代码放在UserForm下。
Public Sub UserForm_Initialize() UserForm1.ListBox1.AddItem "msoShapePentagon" UserForm1.ListBox1.AddItem "msoShapeRectangle" UserForm1.ListBox1.AddItem "msoShapeSmileyFace" End Sub
''''''''''''''''''''''''''''''
Public Sub CommandButton1_Click() MsgBox UserForm1.ListBox1.Value UserForm1.Hide Call Macro2 End Sub
将以下代码放在Power Point 文件的Module1下。
Public Sub Macro1() UserForm1.Show End Sub
''''''''''''''''''''''''''''''
Public Sub Macro2() ActivePresentation.Slides.Add 1, ppLayoutBlank Dim myVariant As Variant myVariant = UserForm1.ListBox1.Value MsgBox myVariant 'This line is okey 'ActivePresentation.Slides(1).Shapes.AddShape Type:=msoShapeRectangle, Left:=0, Top:=0, Width:=480, Height:=100 'This line gives an error ActivePresentation.Slides(1).Shapes.AddShape Type:=myVariant, Left:=0, Top:=0, Width:=480, Height:=100 End Sub
运行Macro1,然后从属于用户窗体一部分的ListBox选择msoShapeRectangle并按CommandButton。
这是错误消息。
Run-time error 13: Type mismatch
这是错误行
ActivePresentation.Slides(1).Shapes.AddShape Type:=myVariant, Left:=0, Top:=0, Width:=480, Height:=100
那么我该如何解决这个错误呢?
"msoShapePentagon"
, a , 与对应值为 51 的枚举成员String
不同。msoShapePentagon
一种选择是使用一个函数将表示形式转换
String
为枚举中的等价形式:请注意,您需要传递此函数 a
String
,而不是 aVariant
。