PowerPoint VBA分组用于选定的形状

发布于 2025-01-28 08:34:23 字数 514 浏览 1 评论 0原文

我正在尝试创建一个子进程,该过程创建一堆图像并多次对其进行分组。 对于第一个循环,一切运行正常,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

I am trying to create a Sub process which create a bunch of image and group it multiple times.
For First loop everything runs OK VBA Code runs as expected, On Second run VBA gives the error "Grouping is disable for selected shapes"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-02-04 08:34:23

将选项显式放在每个模块的顶部并正确声明您的变量总是一个好主意。但是您的代码问题是,范围将形状的名称作为参数,而不是恰好与您给出的对象变量匹配的字符串。这有效:

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

It's always a good idea to put Option Explicit at the top of every module and to properly declare your variables. But the issue with your code is that Range takes the NAMES of shapes as arguments, not strings that happen to match the names you've given the object variables you're using. This works:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文