vba:测试形状是否包含文本框

Mat*_*ech 1 excel vba shapes

我想循环遍历 excel 文件中图表中的所有形状。这原则上适用于

Dim currChart As Chart
Set currChart = Sheets("Diagramm 1")

Dim sShapes As Shape
For Each sShapes In currChart.Shapes

        Debug.Print sShapes.name
        Debug.Print sShapes.TextFrame.Characters.Text

Next sShapes
Run Code Online (Sandbox Code Playgroud)

但是,TextFrame并非所有类型的形状都知道该属性。因此我想测试一个形状是否有一个文本框。我怎样才能做到这一点?

Kaz*_*wor 5

我假设您需要知道形状对象中是否有文本。因此尝试将此代码放在您的循环中For...Next

Debug.Print sShapes.Name
'to check if there is textframe
'but mostly there is
If Not sShapes.TextFrame Is Nothing Then

    'to check if there is text within textframe
    If sShapes.TextFrame2.HasText Then

        Debug.Print sShapes.TextFrame.Characters.Text
    End If
End If
Run Code Online (Sandbox Code Playgroud)

我希望这是你正在寻找的。