0

I having real trouble accessing textbox text via VBA if in a drawing canvas.

Searching this + elsewhere show I should be able to access the text using "TextFrame.TextRange", but it fails, and debug shows no data present (I can't post an image as i'm <10 feedbacks)

Please have a try: - make a new word document, - add a drawing canvas and put a textbox inside with some dummy text - try to access/modify it via VBA Many thanks = )

For Each shp In ActiveDocument.Shapes
    If shp.Type = msoCanvas Then

        For Each canvasitem In shp.CanvasItems
            If canvasitem.Type = msoTextBox Then

                ' NONE OF THESE WORK - WHAT AM I MISSING?
                'Debug.Print canvasitem.TextFrame.TextRange.Text
                'Debug.Print canvasitem.TextFrame.TextRange.Characters.Text

                'If canvasitem.TextFrame2.HasText = True Then _
                 '   Debug.Print canvasitem.TextFrame2.TextRange

            End If
        Next

    End If
Next

2 Answers2

1

This seems to work for me:

For Each shp In ActiveDocument.Shapes
    If shp.Type = msoCanvas Then

        For Each canvasitem In shp.CanvasItems
            If canvasitem.Type = msoTextBox Then
                Debug.Print canvasitem.TextFrame.TextRange.Text
            End If
        Next
    End If
Next
genespos
  • 3,211
  • 6
  • 38
  • 70
  • Hi Thank you for trying it. What version of Office are you using? Strangely, it still wont work for me (Office 2010) even if I make a new fresh clean test doc. "Object Doesn't Support this property of method" with "canvasitem.TextFrame.TextRange.Text" highlighted – Will Turner Jul 28 '15 at 17:11
  • @WillTurner I use Office 2003. Have you checked properties into variable details using "local variables window"? Also try change the name of variable from "canvasitem" to "CItem" (just for an attempt) – genespos Jul 28 '15 at 17:25
  • VB's Local Variables window (and "add watch') is a great suggestion and very helpful! Sadly, it still won't work on Word 2010 – Will Turner Jul 28 '15 at 17:33
  • Explore the treeview of your variable (while debugging) and find a working command – genespos Jul 28 '15 at 17:39
0

I brute forced a work around. It only works for Word 2010 TextBox in a Drawing Canvas. It doesn't work for TextBox outside a Drawing Canvas.

Workaround = to select the text box, then access the text box through the "selection" methods (not great code, but it works).

MICROSOFT .. WHY CAN"T YOU DOCUMENT BETTER! or finish developing before release. Grrr.

I hope this helps someone else

For Each shp In ActiveDocument.Shapes
  If shp.Type = msoCanvas Then

  For Each canvasitem In shp.CanvasItems
     If canvasitem.Type = msoTextBox Then
        ' Word 2003
        ' Debug.Print canvasitem.TextFrame.TextRange.Text

        ' Word 2010 - change the field in a Textbox (in a drawing canvas)
        canvasitem.Select
        For Each fld In Selection.Fields
           If InStr(fld.Code.Text, "STYLEREF 3 \s") _
           Or InStr(fld.Code.Text, "STYLEREF 3") Then _
               fld.Code.Text = "STYLEREF ""Heading 3"" "
        Next

     End If
   Next
   End If
Next