Insert Rows Above Found Text in Table

J

James Pannozzi

Trying to search for the word "Totals"
which occurs towards the end of some tables,
and insert 3 rows above it, then underline two of the
newly inserted rows.

Can't seem to do it without mixing up the range
and causing an infinite loop.

Here's the code

For Each oTbl In objWord.ActiveDocument.Tables
Set oRg = oTbl.Range
With oRg.Find
.ClearFormatting
.Text = "Totals"
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
Do While .Execute
oRg.Rows(1).Range.Select
Selection.InsertRowsAbove 3
' range gets messed up, finds same "Total For" again and again.
Loop
End With
Next

Thanks
Jim
 
M

MSizlak

When you use range.find, the range gets reset to the
range of the found item. So add to your While . . . Wend
loop add the following line

oRg.end=oTbl.end

Moe
 
J

James Pannozzi

Thanks, that's a good idea.

VBScript won't let me do
Set oRg.End = oTbl.Range.End
but there must be some variation thereof once you've invoked the appropriate
objects
and rerembered to use "Set".

Thanks Again
Jim
 
C

Chad DeMeyer

James, instead of:

Do While .Execute
oRg.Rows(1).Range.Select
Selection.InsertRowsAbove 3
Loop

you could try:

..Execute
Do While .Found
oRg.Rows(1).Range.InsertRowsAbove 3
.Execute
.Execute
Loop

This method executes twice in each loop to go first to the instance you
already found, then search for another instance. It uses the .Found
property rather than the return value of .Execute to determine if another
instance was found.

Regards,
Chad
 
J

James Pannozzi

Thanks, that is a GREAT idea.

I'm building some reports in Word with VBScript
which I call from the main app which is written in C++.

Comming to VBA and VBScript from C++
there are a lot of conceptual similarities along
with some rather significant differences - sort of like
a person who has just mastered German suddenly trying
to say a few things in Dutch.


Thanks Again
Jim
 
C

Chad DeMeyer

Glad I could help.

Regards,
Chad


James Pannozzi said:
Thanks, that is a GREAT idea.

I'm building some reports in Word with VBScript
which I call from the main app which is written in C++.

Comming to VBA and VBScript from C++
there are a lot of conceptual similarities along
with some rather significant differences - sort of like
a person who has just mastered German suddenly trying
to say a few things in Dutch.


Thanks Again
Jim
 

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