vba 无法在 Powerpoint 2010 中对某些形状进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16737751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:16:41  来源:igfitidea点击:

Unable to Group some shapes in Powerpoint 2010

vbapowerpointpowerpoint-vba

提问by David Zemens

I am having some difficulty using the .Groupmethod on a ShapeRange.

.GroupShapeRange.

For example, I am not able to Group a table with a textbox, nor am I able to group the slide's title shape with a textbox, etc. I notice that Powerpoint does not allow you to Group certain shapes together, as the "Group" option is disabled on the context menu:

例如,我无法将表格与文本框分组,也无法将幻灯片的标题形状与文本框等分组。我注意到 Powerpoint 不允许您将某些形状分组在一起,作为“分组”选项在上下文菜单上被禁用:

Group is disabled for some shapes

某些形状的组被禁用

I try the following in VBA, which predictably fails:

我在 VBA 中尝试了以下操作,但可以预见地失败了:

Sub TestGroupShapes()

    Dim sld As slide
    Dim tb As Shape
    Dim tb2 As Shape
    Dim tbl As Shape

    Set sld = ActivePresentation.Slides(1)
    Set tbl = sld.Shapes.AddTable(3, 3, 100, 100, 100, 100)
    tbl.Name = "Table"
    Set tb = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 200, 200, 50, 50)
    tb.Fill.Visible = msoCTrue
    tb.Name = "TextBox1"
    Set tb2 = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 300, 300, 50, 50)
    tb2.Fill.Visible = msoCTrue
    tb2.Name = "TextBox2"

    '## This works. I am able to group two textbox shapes together:
    sld.Shapes.Range(Array(tb2.Name, tb.Name)).Group

    '## This fails, I am not able to group a shape with a Table
    '   Also fails, e.g., if I try to group with the slide's Title shape, etc.
    sld.Shapes.Range(Array(tbl.Name, tb.Name)).Group


End Sub

Are there any other methods I might use to "tie" certain shapes together?

我是否可以使用其他任何方法将某些形状“捆绑”在一起?

Update 5/25/2013

2013 年 5 月 25 日更新

The problem is that shapes which a user might copy (Ctrl+V/etc) retain the same .Nameas the original shape. The application I am developing assigns unique IDs to shapes that it creates, but these IDs will not remain unique if the user copies them. For example, some user might "copy" an existing textbox, instead of inserting a newtextbox.

问题是用户可能复制的形状(Ctrl+V/等).Name与原始形状保持相同。我正在开发的应用程序为其创建的形状分配唯一的 ID,但如果用户复制它们,这些 ID 将不会保持唯一。例如,某些用户可能会“复制”现有的文本框,而不是插入新的文本框。

Since the application is required to maintain and update the shapes (tables, textboxes, charts, etc), any shape(s) created by "copying" an existing shape can potentially cause problems, since my application will no longer be able to determine which shapes to update, remove, etc.

由于应用程序需要维护和更新形状(表格、文本框、图表等),通过“复制”现有形状创建的任何形状都可能导致问题,因为我的应用程序将不再能够确定哪个要更新、删除等的形状。

What I was hoping to do is Groupthem together. Although it's not failsafe (the user could still manually Ungroupthem, but I felt like that would be at least an additional safegaurd.

我希望做的是Group他们在一起。虽然它不是故障保护(用户仍然可以手动Ungroup它们,但我觉得这至少是一个额外的安全措施。

回答by dee

It is not possible to group some shapes to one shape. But you can use ShapeRangeto tie different types of shapes together. Then some properties can be set on the shape-range it self, another on particular item. HTH

无法将某些形状组合为一种形状。但是您可以使用ShapeRange将不同类型的形状联系在一起。然后可以在其自身的形状范围上设置一些属性,在特定项目上设置另一个属性。HTH

Sub test()

    Dim tableShape As Shape
    Dim textboxShape1 As Shape
    Dim textboxShape2 As Shape
    Dim myShapeRange As ShapeRange

    With ActivePresentation.Slides(1)
        Set tableShape = .Shapes.AddTable(3, 3, 100, 100, 100, 100)
        tableShape.Name = "Table"

        Set textboxShape1 = .Shapes.AddTextbox(msoTextOrientationHorizontal, 200, 200, 50, 50)
        textboxShape1.Name = "TextBox1"

        Set textboxShape2 = .Shapes.AddTextbox(msoTextOrientationHorizontal, 300, 300, 50, 50)
        textboxShape2.Name = "TextBox2"

        Set myShapeRange = .Shapes.Range(Array("Table", "TextBox1", "TextBox2"))
    End With

    Call FormatShapes(myShapeRange)

End Sub

Private Sub FormatShapes(inputShapeRange As ShapeRange)
    Dim shapeItem As Shape

    With inputShapeRange
        .Align msoAlignLefts, msoFalse
        .AlternativeText = "My group 123"
    End With

    For Each shapeItem In inputShapeRange
        With shapeItem
            .Fill.Visible = msoTrue
            .Fill.Solid
            .Fill.ForeColor.RGB = RGB(255, 0, 0)
            .Fill.TwoColorGradient msoGradientHorizontal, 1
        End With
    Next shapeItem
End Sub

回答by NCWorks Career Center Guilford

When ever you are not able to group your items there is some sort of glitch going on. Follow the steps below to "reactivate the group feature." See if this helps you:

当您无法对您的项目进行分组时,就会出现某种故障。请按照以下步骤“重新激活群组功能”。看看这是否对您有帮助:

  1. Add a new slide to your presentation (you can delete it later)
  2. Insert three shapes of three different colors
  3. Arrange the shapes to attempt to group (the group feature should return)
  4. If it does not return, a temporary fix is to right click on your 3 selected items, then choose the "picture" Paste option (looks like clip board and picture icon together) this should group the items together as well.
  1. 向演示文稿添加新幻灯片(您可以稍后将其删除)
  2. 插入三种不同颜色的三种形状
  3. 排列形状以尝试分组(分组特征应该返回)
  4. 如果它没有返回,临时解决方法是右键单击您选择的 3 个项目,然后选择“图片”粘贴选项(看起来像剪贴板和图片图标),这也应该将这些项目组合在一起。

Hope this helps!

希望这可以帮助!

回答by Tim

Here is a long-way to do it, but it'll work (at least it did for me, :D).

这是一个很长的路要走,但它会起作用(至少对我来说是这样,:D)。

What you'll have to do is make your tables then right click on them separately; what you want to do is save your tables as a picture (as a JPEG will work great for this) either to your desktop or some other convenient file. Then put your tables (in picture form) back into the slide. You should be able to group all your things together now.

您需要做的是制作表格,然后分别右键单击它们;您想要做的是将您的表格另存为图片(因为 JPEG 对此非常有用)到您的桌面或其他一些方便的文件中。然后将您的表格(以图片形式)放回幻灯片中。您现在应该能够将所有东西组合在一起。

It is a PITA, but at least it'll work. Best of luck!

这是一个 PITA,但至少它会起作用。祝你好运!