对所选形状禁用Powerpoint VBA分组



我试图创建一个子进程,创建一堆图像和组它多次。对于第一次循环,一切都运行正常VBA代码按预期运行,在第二次运行VBA给出错误"分组是禁用选定形状">

Sub PPT_AddShape14
x = 8 * (i - 1) + 170 * (i - 1)
y = 0
Set sh1 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 8, 170, 170)
Set sh2 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 178, 170, 30)
Set sh3 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 208, 170, 190)
vslide.Shapes.Range(Array("sh1", "sh2", "sh3")).Group.Name = Str(x)
Set Tiles = vslide.Shapes(Str(x))
End Sub

将Option Explicit放在每个模块的顶部并正确声明变量总是一个好主意。但你的代码的问题是,范围采取形状的名称作为参数,而不是字符串,恰巧匹配的名称,你已经给你正在使用的对象变量。如此:

Option Explicit
Sub PPT_AddShape14()
Dim i As Long
Dim x As Long
Dim y As Long
Dim sh1 As Shape
Dim sh2 As Shape
Dim sh3 As Shape
Dim vslide As Slide
Set vslide = ActivePresentation.Slides(1) ' as test
Dim Tiles As Shape
x = 8 * (i - 1) + 170 * (i - 1)
y = 0
Set sh1 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 8, 170, 170)
Set sh2 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 178, 170, 30)
Set sh3 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 208, 170, 190)
vslide.Shapes.Range(Array(sh1.Name, sh2.Name, sh3.Name)).Group.Name = Str(x)
Set Tiles = vslide.Shapes(Str(x))
End Sub

最新更新