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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16171219/
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 20:47:10  来源:igfitidea点击:

vba: test if shape contains a textframe

vbaexcel-vbashapesexcel

提问by Matthias Pospiech

I want to loop over all shapes in a chart in an excel file. This works in principle with

我想循环遍历 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

However, the property TextFrameis not known by all type of shapes. therefore I want to test if a shape has a textframe. How can I do that?

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

回答by Kazimierz Jawor

I assumed that you need to know if there is text within your shape object. Therefore try to put this code within your loop For...Next:

我假设您需要知道形状对象中是否有文本。因此尝试将此代码放在您的循环中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

I hope it is what you are looking for.

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