Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

excel - vba: test if shape contains a textframe

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

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 TextFrame is not known by all type of shapes. therefore I want to test if a shape has a textframe. How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...