Share via

How to add text into multiple Textboxes using VBA?

Anonymous
2023-12-04T19:53:29+00:00

Hello everyone,

Hopefully someone has an answer to this:

I am currently trying to go through and indicate where textboxes are in a Word document by having a macro that adds the following into the textbox [BeginTextbox] at the beginning of the text and [EndTextbox] at the end of the text.

The only issue is that the VBA code I am using won't go to the next textbox in the document. Any help on the code would be appreciated.

Dim oShape As Shape

If ActiveDocument.Shapes.Count > 0 Then 

    For Each oShape In ActiveDocument.Shapes 

        If oShape.AutoShapeType = msoShapeRectangle Then 

            If oShape.TextFrame.HasText = True Then 

                oShape.TextFrame.TextRange.InsertBefore "[BeginTextbox]" 

                 oShape.TextFrame.TextRange.InsertAfter "[EndTextbox]" 

                 Exit For

            End If 

        End If 

    Next oShape 

End If 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2023-12-04T20:15:23+00:00

    Just in case the artificial idiot knows something I do not, I ran your question through it and got the following response in case it helps

    To add text into multiple textboxes using VBA in Word, you can modify the code you provided as follows:

    
    Sub AddTextToTextBoxes()
    
        Dim oShape As Shape
    
        For Each oShape In ActiveDocument.Shapes
    
            If oShape.Type = msoTextBox Then
    
                oShape.TextFrame.TextRange.InsertBefore "[BeginTextbox]"
    
                oShape.TextFrame.TextRange.InsertAfter "[EndTextbox]"
    
            End If
    
        Next oShape
    
    End Sub
    
    

    This code loops through all the shapes in the document and checks if the shape is a textbox. If it is, it adds the specified text before and after the existing text in the textbox. Note that this code assumes that the textboxes are not nested within other shapes.

    That does not seem that different from what you already tried but it is looking for a shape actually labelled as a Text Box.

    Was this answer helpful?

    0 comments No comments
  2. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2023-12-04T20:05:09+00:00

    I do not have an answer for you, I'm afraid, because I tend to avoid textboxes. They can cause problems.

    Why are you using Text Boxes as the holder?

    Why not Content Controls, perhaps in Table Cells or formatted with borders?

    You may want to iterate through the Shapes collection backwards, from last to first. I do not know that this is your problem but it can be when making changes.

    For some code examples dealing with iterating through bookmarks or Content Controls, see:

    Was this answer helpful?

    0 comments No comments