TextBox and Tables - a couple of questions

S

Steve Cronin

Folks;

What I want to do is essentially 'mail-merge' but the user is not going
to be placing merge tags.
They will simply put placeholders that I will substitute with data in
my script.
They will be placing the text in normal layout, inside of textboxes,
and inside of tables.
Could be some or all of the above.

How do I determine if there are any TEXT BOXES and then iterate over
them changing .Text any 'abc' to 'xyz'? (TextBoxes just puzzle me...)

For Tables I've figured out that I can do:
With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
For j = 1 To .Columns.Count
[do something with .Cell(i,j).Range.Text]
Next j
Next i
End With

But:
1) How do I determine how many tables there are and then iterate over
them successively?
2) Is there a smarter way than walking every thru cell and checking for
..Range.Text?

In the general layout (not in a TextBox or a Table) is this the
preferred solution:
With Selection.Find.Text ="abc"
..Replacement.Text = "xyz""
..Forward = True
..Wrap = 1
..Format = False
End With
ReplaceAll()

Thanks for any insight!!
Steve
 
J

Jezebel

Unless you are concerned that the placemarker text may occur outside your
tables and textboxes and should *not* be substituted in that context, you
can just use Find and Replace: if the Search domain is set to ALL, that will
catch the textboxes also. There's no need to deal with tables and textboxes
individually.


If you really want to do it the hard way, you can iterate tables using code
like

Dim pTable as Word.Table
For each pTable in ActiveDocument.Tables
...
Next

There's no need to deal with each cell separately, either. You can just use
pTable.Range to refer to the whole thing, eg

With pTable.Range.Find
.Text = "..."
.Replace.Text = "..."
.Execute Replace:=wdReplaceAll
End with


To iterate textboxes, use something like

Dim pTextBox as Word.Range

on error resume next 'Ignore error if there are NO textboxes
Set pTextBox = ActiveDocument.StoryRanges(wdTextFrameStory)
on error goto 0

Do until pTextBox is nothing
.... do whatever
set pTextBox = pTextBox.NextStoryRange
Loop
 
J

Jezebel

On the Find and Replace dialog, click more, and select All. From VBA use
wdFindContinue.
 

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