用于在 Excel 工作簿中取消形状(特别是文本框)分组的宏

问题描述 投票:0回答:1

我有一个宏,用于翻译 Excel 工作簿中文本框形状内的文本,但如果任何文本框分组在任何工作表中,则该宏会失败。有没有一种简单的方法可以通过 VBA 宏取消工作簿中所有形状的分组?

这是我发现在 Microsft Word 中有效,但在 Excel 中不起作用的示例:

Sub Ungroup()
Dim xNmbr As Integer
With ActiveDocument
    For xNmbr = .Shapes.Count To 1 Step -1
    .Shapes(xNmbr).Select
        Set thisshape = .Shapes(xNmbr)
        With thisshape.WrapFormat
       .Type = wdWrapSquare
    If thisshape.Type = msoGroup Then thisshape.Ungroup
    End With
    Next
End With
End Sub
excel vba textbox grouping shapes
1个回答
0
投票

在 Excel 中取消形状分组与 Word 非常相似。当然,您在工作表上循环形状,而不是文档。

Sub ungroupShapes(ws As Worksheet)
    Dim sh As Shape
    For Each sh In ws.Shapes
        If sh.Type = msoGroup Then
            sh.Ungroup
        End If
    Next
End Sub

但也许您想保留小组结构。对于一个组,您可以使用

GroupItems
遍历该组内的形状。也许使用如下递归例程

Sub handleShapes(shapes As Variant)
    Dim sh As Shape
    DoEvents
    For Each sh In shapes
        If sh.Type = msoGroup Then
            handleShapes sh.GroupItems
        Else
            DoSomethingWithTheShape sh
        End If
    Next
End Sub

Sub DoSomethingWithTheShape(sh As Shape)
    Debug.Print sh.Name, sh.Type
End Sub

只需用类似的东西调用例程即可

handleShapes ThisWorkbook.sheets(1).Shapes
' or
handleShapes ActiveSheet.Shapes
' or
For Each ws in Thisworkbook.Worksheets
    handleShapes ws
Next ws

不幸的是,工作表的

Shapes
的类型与组的子形状不同,因此
handleShapes
的参数需要为
Variant
类型。

DoSomethingWithTheShape
只是您要在工作表上执行的操作(翻译文本)的占位符。

© www.soinside.com 2019 - 2024. All rights reserved.