Desperately need help for Word VBA to Fix Tables

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
Hi,

I have hundreds of tables in a word document and for some reason, their formatting are all wrong. I desperately need help to put in a VBA script to fix them. I already tried searching through the threads to modify the answer but I can't get it to work right.

There are merged cells in some tables. I need all the tables to have these formatting:

1. table style = table grid
2. first row of each table to be bold in font
3. repeat first header row

Please help!!!
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Try:
Code:
Sub FixTblFmts()
Application.ScreenUpdating = False
Dim Tbl As Table, i As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Style = "Table Grid"
    .Range.Cells(1).Range.Rows.HeadingFormat = True
    For i = 1 To .Range.Cells.Count
      If .Range.Cells(i).RowIndex = 1 Then
        .Range.Font.Bold = True
      Else
        Exit For
      End If
    Next
  End With
Next
Application.ScreenUpdating = True
End Sub
 

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
Thanks! I tried and it turns all the rows in the table bold in font. I only need the first row to be bold in font. The other formatting are all good!



Try:
Code:
Sub FixTblFmts()
Application.ScreenUpdating = False
Dim Tbl As Table, i As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Style = "Table Grid"
    .Range.Cells(1).Range.Rows.HeadingFormat = True
    For i = 1 To .Range.Cells.Count
      If .Range.Cells(i).RowIndex = 1 Then
        .Range.Font.Bold = True
      Else
        Exit For
      End If
    Next
  End With
Next
Application.ScreenUpdating = True
End Sub
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Unless your tables are using a Style in one or more cells in the first row with the 'update automatically' attribute applied, and the same Style is used on the other rows, I can't see how that's possible - the macro only applies bold formatting to cells whose RowIndex is 1 (i.e. the first row).
 

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
Ehhh....I am not really sure what you meant but I have not done anything except that some some cells on the first row might be merged.


Unless your tables are using a Style in one or more cells in the first row with the 'update automatically' attribute applied, and the same Style is used on the other rows, I can't see how that's possible - the macro only applies bold formatting to cells whose RowIndex is 1 (i.e. the first row).
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Every paragraph in a Word document uses a paragraph Style. What paragraph Styles do the cells in the first row use? If you inspect the Style definitions for those paragraphs, do any have the 'Automatically update' attribute checked? Even a single paragraph in the first row of any of your tables that has that attribute checked will cause all other paragraphs using the same Style to be bolded throughout your document!
 

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
Hi

No, I don't have paragraph style set as attribute "automatically updated" checked.



Every paragraph in a Word document uses a paragraph Style. What paragraph Styles do the cells in the first row use? If you inspect the Style definitions for those paragraphs, do any have the 'Automatically update' attribute checked? Even a single paragraph in the first row of any of your tables that has that attribute checked will cause all other paragraphs using the same Style to be bolded throughout your document!
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Can you upload a relevant portion of the document (obfuscate/delete anything sensitive)? You can do that via the 'Upload a File' button. That way I might be able to better understand what is happening.
 

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
I tried to upload but the website don't allow. It kept saying it does not have an allowed extension -
test.docx



Can you upload a relevant portion of the document (obfuscate/delete anything sensitive)? You can do that via the 'Upload a File' button. That way I might be able to better understand what is happening.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
OK, Try:
Code:
Sub FixTblFmts()
Application.ScreenUpdating = False
Dim Tbl As Table, i As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Style = "Table Grid"
    .Range.Cells(1).Range.Rows.HeadingFormat = True
    For i = 1 To .Range.Cells.Count
      If .Range.Cells(i).RowIndex = 1 Then
        .Range.Cells(i).Range.Font.Bold = True
      Else
        Exit For
      End If
    Next
  End With
Next
Application.ScreenUpdating = True
End Sub
 

Jen

Joined
Jan 17, 2017
Messages
7
Reaction score
0
Thanks! It works now!


OK, Try:
Code:
Sub FixTblFmts()
Application.ScreenUpdating = False
Dim Tbl As Table, i As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Style = "Table Grid"
    .Range.Cells(1).Range.Rows.HeadingFormat = True
    For i = 1 To .Range.Cells.Count
      If .Range.Cells(i).RowIndex = 1 Then
        .Range.Cells(i).Range.Font.Bold = True
      Else
        Exit For
      End If
    Next
  End With
Next
Application.ScreenUpdating = True
End Sub
 

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