Monceber
Monceber

Reputation: 151

Formating all tables in file with style

I'm working with some kind of documentation at the moment, which consists of lots and lots of tables. And as you may have guessed by now, I need to format them with a certain style. So, there's a VBA macro exclusively made for that purpose - it creates required style and then applies it to all tables in the file. But now it seems to be a problem when I'm working with large document. So, let's see working code, ommiting part, where style is created:

Dim oTable As Table
For Each oTable In ActiveDocument.Tables
oTable.Select
Selection.Tables(1).ApplyStyleDirectFormatting ("FooStyle")
Selection.Tables(1).AutoFitBehavior (wdAutoFitFixed)
Selection.Tables(1).ApplyStyleRowBands = True
With Selection.Tables(1).Borders
    .InsideLineStyle = wdLineStyleSingle
    .InsideLineWidth = wdLineWidth025pt
    .OutsideLineStyle = wdLineStyleSingle
    .OutsideLineWidth = wdLineWidth025pt
End With

'Make a list for last row of the table
Selection.Tables(1).Cell(Row:=8, Column:=1).Range.Select
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdOutlineNumberGallery).ListTemplates(2)
Next

So this code works nicely for documents less than 600 pages. Otherwise it stops at line

.InsideLineStyle = wdLineStyleSingle

with run-time error 4605 telling stories about memory and disk space. I've done some research and found this awesome thread telling about downsides of Selection. Then following given advice and changed macro's code a bit, resulting in following:

Dim lTbl As Long
For lTbl = 1 To ActiveDocument.Tables.Count
ActiveDocument.Tables(lTbl).ApplyStyleDirectFormatting ("BarStyle")
ActiveDocument.Tables(lTbl).AutoFitBehavior (wdAutoFitFixed)
ActiveDocument.Tables(lTbl).ApplyStyleRowBands = True
With ActiveDocument.Tables(lTbl).Borders
    .InsideLineStyle = wdLineStyleSingle
    .InsideLineWidth = wdLineWidth025pt
    .OutsideLineStyle = wdLineStyleSingle
    .OutsideLineWidth = wdLineWidth025pt
End With

'Make a list for last row of the table
ActiveDocument.Tables(lTbl).Cell(Row:=8, Column:=1).Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdOutlineNumberGallery).ListTemplates(2)
Next

Yeah, creepily long line. Nope, nothing changed. Seems like there're no more selections, but still the same error at the same place. So then i tried changing part where debugger stopped when error appeared:

For Each oCell In ActiveDocument.Tables(lTbl).Range.Cells
    oCell.Borders.OutsideLineStyle = wdLineStyleSingle
    oCell.Borders.OutsideLineWidth = wdLineWidth025pt
    Next

To no avail - still got the same problem. At this point I decided to do some experiments and removed next to last line of the code (that long-a** line)... and voila - macro works like a charm, no errors, no complaints, nothing. For arguments sake I tried making same changes to first version of this macro and results was the same - seems like it was that line's fault all along. So leaving it like this is not a solution, as the numbered list in last row is a must. Then, what should I do to this macro that it doesn't pop up this error?

Upvotes: 1

Views: 1707

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

That kind of problem can be solved by splitting task into two loops. First iteration will change the table formatting. Second iteration will add ListFormat where expected.

Of course, as a result we get slower subroutine but finally we get what we need.

Upvotes: 1

Related Questions