A macro to bold first row in word tables

N

Nick

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/tutorial...5-e67a1c071601/rounded-corner-content-ed.aspx
 
D

DaveLett

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
 
N

Nick

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
'''remove bold face formatting from the first ro
'''of every table except for the first tabl
Dim lTbl As Lon

For lTbl = 2 To ActiveDocument.Tables.Coun
ActiveDocument.Tables(lTbl).Rows(1).Range.Font.Bold = Fals
Next lTb

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/tutorial...ef-7dd29b6ae909/build-a-c-notifyicon-bal.aspx
 
D

DaveLett

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
 
N

Nick

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/tutorial...8c-1d4261d01e86/how-to-get-rid-of-the-xx.aspx
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

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

For TableIndex = 2 To ActiveDocument.Tables.Count
 
Joined
Apr 25, 2012
Messages
1
Reaction score
0
Tables within Tables

How can I SHADE the first row of any/all tables within a Word document? Some of our tables may have an If/Then table inside of a cell and it's not catching those.

Thanks,

Matthew
 

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