excel HasTextFrame -无法访问组对象的文本

wkyowqbh  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(98)

我有20个工作簿,不能更新文本(标题1金额1...)以下组对象添加在图表顶部并出现在每张表。
要更新的组对象的文本
出现的错误显然是'If .Item(1).HasTextFrame Then'行。你能给我提供线索吗?谢谢你的时间和考虑

Sub B Macro()
'
' B Macro
'
Dim wks As Worksheet
Dim numShapes, numAutoShapes, i As Long

    For Each wks In Worksheets
        With wks.Shapes
            numShapes = .Count
            
            If numShapes > 1 Then

                numTextShapes = 0
                For i = 1 To numShapes
                    If .Item(1).HasTextFrame Then
                        content = .Item(1).TextFrame.Characters.Text
MsgBox "Text: " & sMsg
                    End If
                Next
            End If
            End With
    Next wks
    '
End Sub
e1xvtsh3

e1xvtsh31#

HasTextFrameShapeRange而不是Shape的属性。如果你只是想检查文本内容,你可以这样做:

Sub BMacro()

    Dim wks As Worksheet, numTextShapes As Long, shp As Shape
    Dim numShapes As Long, itm As Shape
    
    For Each wks In Worksheets
        numShapes = wks.Shapes.Count
        If numShapes > 1 Then
            numTextShapes = 0
            For Each shp In wks.Shapes
                If shp.Type <> msoGroup Then        'not a group?
                    Debug.Print ShapeText(shp)
                Else
                    For Each itm In shp.GroupItems  'loop grouped items
                        Debug.Print ShapeText(itm)
                    Next
                End If
            Next shp
        End If
    Next wks
End Sub

Function ShapeText(shp As Shape) As String
    If Not shp.HasChart Then   'ignore chartobjects
        On Error Resume Next   'ignore error if no textframe...
        ShapeText = shp.TextFrame.Characters.Text
    End If
End Function

相关问题