Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A macro to bold first row in word tables

2,246 views
Skip to first unread message

Nick

unread,
Jan 19, 2010, 12:54:21 PM1/19/10
to
I have been having an issue with applying new styles to existing tables in Word 2007 stripping away the bolding of the first row which contains the headings for all the columns. Is there a way to loop through the tables and apply this formatting? Also the first table in the document does not need the bold, is there a way to exclude the loop from applying to the first table in the word document? Thanks for any help that can be provided.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Rounded corner content editor web part with custom colors
http://www.eggheadcafe.com/tutorials/aspnet/462e29fe-2258-45c0-bd65-e67a1c071601/rounded-corner-content-ed.aspx

DaveLett

unread,
Jan 19, 2010, 2:10:01 PM1/19/10
to
Hi Nick,

I think you're looking for something like the following:
'''remove bold face formatting from the first row
'''of every table except for the first table
Dim lTbl As Long

For lTbl = 2 To ActiveDocument.Tables.Count
ActiveDocument.Tables(lTbl).Rows(1).Range.Font.Bold = False
Next lTbl

HTH,
Dave

Nick

unread,
Jan 19, 2010, 3:50:01 PM1/19/10
to
Dave,

Thanks a lot the code works great. I am running into an error on a few of the tables.

Run-time error '5991';

Cannot access individual rows in this collection because the table has vertically merged cells.

None of the first row has vertically merged cells but later on in a few tables this is the case.

I was wondering if there was a known work around or how I would go about excluding individual tables in the macro. Thanks again for your help.

DaveLett wrote:

Hi Nick,I think you are looking for something like the following:'''remove
19-Jan-10

Hi Nick,

I think you are looking for something like the following:

HTH,
Dave

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice

Build a C# NotifyIcon BalloonTip Scheduled Outlook Mail Checker
http://www.eggheadcafe.com/tutorials/aspnet/0235490b-a901-42b4-9eef-7dd29b6ae909/build-a-c-notifyicon-bal.aspx

DaveLett

unread,
Jan 19, 2010, 4:47:02 PM1/19/10
to
Hi Nick,
Yes, it's known issue with merged cells. You can try the following:

Dim lTbl As Long
Dim lCl As Long

For lTbl = 2 To ActiveDocument.Tables.Count

With ActiveDocument.Tables(lTbl)
For lCl = 1 To .Range.Cells.Count
If .Range.Cells(lCl).RowIndex = 1 Then
.Range.Cells(lCl).Range.Font.Bold = True
End If
Next lCl
End With
Next lTbl

HTH,
Dave

Nick

unread,
Jan 20, 2010, 8:54:15 AM1/20/10
to

Dave,

I was wondering if you could recommend a quick way to modify the following code so it also starts on the second table, like was done with the bold code.

Dim myTable As Table
For Each myTable In ActiveDocument.Tables
myTable.Select
Selection.Style = ActiveDocument.Styles("Custom Table")
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = "12"
Selection.Rows.Alignment = wdAlignRowCenter
Next myTable
ActiveDocument.Repaginate

Thanks again for all your help.

DaveLett wrote:

Hi Nick,Yes, it is known issue with merged cells.
19-Jan-10

Hi Nick,
Yes, it is known issue with merged cells. You can try the following:

Dim lTbl As Long
Dim lCl As Long

For lTbl = 2 To ActiveDocument.Tables.Count
With ActiveDocument.Tables(lTbl)
For lCl = 1 To .Range.Cells.Count
If .Range.Cells(lCl).RowIndex = 1 Then

..Range.Cells(lCl).Range.Font.Bold = True


End If
Next lCl
End With
Next lTbl

HTH,
Dave

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice

HOW TO GET RID OF THE "XXexmodulae.exe" Trojan
http://www.eggheadcafe.com/tutorials/aspnet/4d1b500f-23ce-4a5f-a18c-1d4261d01e86/how-to-get-rid-of-the-xx.aspx

Jay Freedman

unread,
Jan 20, 2010, 10:38:42 AM1/20/10
to
Replace the For Each loop with one that uses the index into the Tables
collection, and start at 2 -- something like this:

Dim TableIndex As Long
For Each TableIndex = 2 To ActiveDocument.Tables.Count
ActiveDocument.Tables(TableIndex).Select
' then the formatting as before
Next TableIndex

Also, I'd recommend *not* selecting the table and using Selection to do the
formatting. VBA gives you access to things that aren't selected, with the
advantage that the document doesn't have to scroll and redraw the screen,
which is a slow operation. Sample code:

Dim TableIndex As Long
Dim myTable As Table
For Each TableIndex = 2 To ActiveDocument.Tables.Count
Set myTable = ActiveDocument.Tables(TableIndex)
With myTable
.Range.Style = ActiveDocument.Styles("Custom Table")
.Range.Font.Name = "Times New Roman"
.Range.Font.Size = 12
.Rows.Alignment = wdAlignRowCenter
End With
Next TableIndex

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Jay Freedman

unread,
Jan 20, 2010, 10:45:46 AM1/20/10
to

Oops, copy/paste error. The For statement should not have an "Each" in it.
Instead, use

For TableIndex = 2 To ActiveDocument.Tables.Count

Jay Freedman wrote:
> Replace the For Each loop with one that uses the index into the Tables
> collection, and start at 2 -- something like this:
>
> Dim TableIndex As Long
> For Each TableIndex = 2 To ActiveDocument.Tables.Count
> ActiveDocument.Tables(TableIndex).Select
> ' then the formatting as before
> Next TableIndex
>
> Also, I'd recommend *not* selecting the table and using Selection to
> do the formatting. VBA gives you access to things that aren't
> selected, with the advantage that the document doesn't have to scroll
> and redraw the screen, which is a slow operation. Sample code:
>
> Dim TableIndex As Long
> Dim myTable As Table
> For Each TableIndex = 2 To ActiveDocument.Tables.Count
> Set myTable = ActiveDocument.Tables(TableIndex)
> With myTable
> .Range.Style = ActiveDocument.Styles("Custom Table")
> .Range.Font.Name = "Times New Roman"
> .Range.Font.Size = 12
> .Rows.Alignment = wdAlignRowCenter
> End With
> Next TableIndex
>
>

0 new messages