Results 1 to 8 of 8

Thread: Modify a table in the header with VBA in Word

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by gmayor View Post
    OK, you cannot use that method when the table has merged cells. The following however will work with that table whichever header it is in (It is in the first page header in your sample)
    If you want to select which header, then you will need to replace the line

    oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784
    with
    oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784
    Sub Macro1()
        Dim oSection As Section
        Dim oHeader As HeaderFooter
        Dim oTable As Table
        For Each oSection In ActiveDocument.Sections
            For Each oHeader In oSection.Headers
                If oHeader.Exists Then
                    If oHeader.Range.Tables.Count > 0 Then
                        Set oTable = oHeader.Range.Tables(1)
                        If oHeader.Range.Tables.Count > 0 Then
                            Set oTable = oHeader.Range.Tables(1)
                            oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784
                        End If
                    End If
                End If
            Next oHeader
        Next oSection
        lbl_Exit:
        Set oSection = Nothing
        Set oHeader = Nothing
        Set oTable = Nothing
        Exit Sub
    End Sub
    Thank you my friend.

    With your help I finally have finnish my macro and it works perfectly.

    If you ever come to Pamplona i will invite you to have dinner.


    Thanks!!
    Last edited by Aussiebear; 01-02-2025 at 03:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •