Apply a user-defined paragraph style to the first paragraph of Cell (1,1) of all the tables of the current document

Dear Experts:

below macro (Courtesy of MS Word 'Sage' Graham Skan) applies a user-defined paragraph style to the table cell that is located on the intersection of Row 4 and Column 2 (see line 10).

Could somebody please tweak this code due to a new requirement. The requirement is as follows:

Apply the heading Style "MyParaStyle" to the first paragraph of table cell (1,1), i.e. table cell (1,1) contains several paragraphs and only the first one should get the user-defined paragraph style applied.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas



Sub ApplyStyle()
    Dim tbl As Table
    
     If MsgBox("Would you like to apply the 'MyParaStyle' to the item name?" & vbCrLf & vbCrLf & _
     "This will be the basis for the Adobe Acrobat bookmarks!", vbQuestion + vbYesNo, "Apply Style to Product Name") = vbNo Then
        Exit Sub
        End If
    
    For Each tbl In ActiveDocument.Tables
        tbl.Cell(4, 2).Range.Style = "MyParaStyle"
    Next tbl
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Try this.
Sub ApplyStyle()
    Dim tbl As Table
    
     If MsgBox("Would you like to apply the 'MyParaStyle' to the item name?" & vbCrLf & vbCrLf & _
     "This will be the basis for the Adobe Acrobat bookmarks!", vbQuestion + vbYesNo, "Apply Style to Product Name") = vbNo Then
        Exit Sub
        End If
    
    For Each tbl In ActiveDocument.Tables
        tbl.Cell(1, 1).Range.Paragraphs(1).Style = "MyParaStyle"
    Next tbl
End Sub

Open in new window

0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Norie, great this did the trick. Thank you very much for it.

Regards, Andreas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.