VBA to Remove PageBreaks from Between Tables

J

James Pannozzi

I have built some tables by converting
tab delimited text files to table.

After each convert to table, I insert a pagebreak with

MyRange.InsertBreak wdPageBreak

thus starting each new table on a new page.

I tried iterating across the several tables
and keep getting "required element not found in collection"
(see code below)
but when I have just one table the text bolding works fine.

Is there a way to search and remove
these pagebreaks so that the
several tables all become 1 table
so that I can search for some text
and boldface rows containing it easily without tripping over mixed up ranges
or whatever the hell it is that is giving me that message.

After that I would just re-insert the pagebreaks
thus restoring the paging.

Here is code I am using to do the boldfacing
after the tables are being built:

For T = 1 to objWord.ActiveDocument.Tables.Count
'Boldface the row whoose first column has the text "Special Fee"
With objWord.Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.MatchWildcards = True
.Text = "Special Fee" 'find and boldface all rows with
occurences of this text
Do While .Execute
Set oRng =
objWord.ActiveDocument.Range(objWord.Selection.Range.Start,objWord.Selection
..Range.End)

objWord.ActiveDocument.Tables(T).Rows(oRng.Information(wdEndOfRangeRowNumber
)).Range.Font.Bold = True
Loop
End With
End With
Next


After doing the boldfacing I'd like to
restore the pagebreaks.
 
J

James Pannozzi

Instead of starting the loop at the start of each table
I'm going back to the start of the whole document.

It so happens that the first table has more rows than the 2d or 3d
so it gives me the a range for the row of a found item in table 1 that
doesn't exist in table 2.

OK. this is finally making some sense.

J
 
J

Jay Freedman

Hi James,

You don't have to play games with the page breaks. A properly written macro
will bold the rows in any number of tables separated by any kind or amount
of text.

The problem with the macro you posted is that each iteration of the For T
loop starts by sending the Selection back to the start of the document and
searching from there. That could be overcome, but the more fundamental
problem is in using the selection at all. Your code will be smaller and run
faster if you work entirely with a Range object, as follows:

Public Sub BoldFees()
Dim oRg As Range
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
Set oRg = oTbl.Range
With oRg.Find
.ClearFormatting
.Text = "Special Fee"
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
Do While .Execute
oRg.Rows(1).Range.Bold = True
Loop
End With
Next oTbl
End Sub

There are two "unobvious" facts that are keys to how this macro works. One
is that each time the .Execute finds the text, it causes oRg to cover
exactly the found text. The other is that once oRg does cover the found
text, the expression oRg.Rows(1) identifies the row that contains the text
(because that is the first row within oRg). You don't have to figure out the
index of the table within ActiveDocument, or the index of the found row
within that table -- that's already taken care of.
 
J

James Pannozzi

Wow, thanks!

I just spent a VERY unpleasant day and a half, doing it the wrong way.

The other thing I tripped on was that there was no Column bolding
in the Word object model, although there was a Row bolding feature,
but Dave Lett came up with a cool way around that one too.

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