Selecting a TextBox using a loop variable

T

Tim Golobic

I have a document with several TextBoxes that I need to select using
the value of a loop, since I can't seem to determine each box's name.
How do I address the TextBox within the loop? For example, I have:

For MyLoop = 0 to 1000
TextBox & MyLoop.Select
Next

Which I know is wrong, but don't know how to correct.

Thanks

Tim
 
G

Greg Maxey

Tim,

I don't really understand your question. Textboxes are inserted in Word
documents as shapes. You can get the name of each Text Box and work with a
named Text Box like this:

Sub ScratchMacro()
Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoTextBox Then
MsgBox oShp.Name
End If
Next
ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange.Text = "Hello Tim"
End Sub
 
T

Tim Golobic

Greg,

Thank you, perfect. You have given me the context to succeed.

The whole story is this (I was trying not to overload the original
message with unnecessary detail):

I have a "yearbook" I am compiling for our 20-year HS reunion.
Classmates fill out a survey on line tied to a Google spreadsheet, ie:
kids, job, memories, etc. I decided to play around with a data merge
in Word. I got everything working great except that I wanted to
include each person's yearbook photo alongside their bio. After much
work I finally found the INCLUDEPICTURE field (Using Word v.X for
Mac). But for unknown reasons, you can't adjust the picture format
with this field, otherwise it removes the field and leaves the
picture, not the link. I want the text to wrap around the picture. I
found that by either inserting the picture in a Text Box, I can format
the Text Box to wrap without affecting the picture link. But after the
merge, the Select All and Update Fields skipped over the pictures
inside the Text Boxes. Since I don't want to select each of them
indivisually to update the picture (could get to 100 of those), I
wanted to create a macro the would select the box, update the field,
and keep on loopin'. Which is now accomplished with:

Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoTextBox Then
oShp.Select
Selection.Fields.Update
End If
Next

Thanks again,

Tim
 
G

Greg Maxey

Tim,

Glad I could help.

The reason the "Select All" doesn't include the textboxes is because
textboxes are not part of the MainText storyrange of the document. While
they appear normally on the screen and on paper, they physically reside in
the TextFrame story range.

You can work with fields in the TextFrame story like this without having to
physically or with code select each TextBox.

Sub UpdateFieldsinTBs()
Dim rngStory As Word.Range
Set rngStory = ActiveDocument.StoryRanges(wdTextFrameStory)
Do
rngStory.Fields.Update
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top