Share via

VBA Word Footers - Enabling a Border

Anonymous
2014-07-28T12:45:26+00:00

The following code extract successfully writes a new footer to a Word document:

        For Each oFooter In oSec.Footers

            If oFooter.Exists Then

                Set rngFooter = oFooter.Range

                With rngFooter

                    With .ParagraphFormat.TabStops

                        .ClearAll

                        .Add CentimetersToPoints(posAlignTabCentre), wdAlignTabCenter

                        .Add CentimetersToPoints(posAlignTabRight), wdAlignTabRight

                    End With

                    .Font.Name = "Arial"

                    .Font.Size = "10"

                    .Font.Color = wdColorDarkBlue

                    .Text = strTitle & vbTab & strVersion & vbTab & "Page "

                    .Collapse wdCollapseEnd

                    .Fields.Add rngFooter, wdFieldPage, , False

                    .Start = oFooter.Range.End

                    .Text = " of "

                    .Collapse wdCollapseEnd

                    .Fields.Add rngFooter, wdFieldNumPages, , False

                    .Start = oFooter.Range.End

                    .Text = vbCr & vbCr & strCopyright & vbTab & vbTab & strClassification

                    .Borders.Enable = True

                End With

            End If

        Next oFooter

The footer is presented as shown below. Each row in the 'table' has a border.


|  strTitle                               strVersion                             Page x of y      |


|                                                                                                             |


|  strCopyright                                                              strClassification  |


I wish to change this to show the border around the 3 'cells' in the first row only.

I have attempted to illustrate this below


|  strTitle                |               strVersion                 |            Page x of y    |


  strCopyright                                                              strClassification  

I need to change .Borders.Enable = True

How can I achieve this?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-07-29T12:14:51+00:00

In that case Plan B, use the table for the bit with borders and text for the rest

    For Each oFooter In oSec.Footers

        If oFooter.Exists Then

            Set RngFooter = oFooter.Range

            With RngFooter

                .Style = "Footer"

                .Text = vbCr & vbCr & strCopyright & vbTab & vbTab & strClassification

                .Font.name = "Arial"

                .Font.Size = "10"

                .Font.Color = RGB(0, 0, 128)

                .Collapse wdCollapseStart

                Set oTable = RngFooter.Tables.Add(RngFooter, 1, 3)

                With oTable

                    .Range.Font.name = "Arial"

                    .Range.Font.Size = "10"

                    .Range.Font.Color = RGB(0, 0, 128)

                    .Cell(1, 1).Range.Text = strTitle

                    .Cell(1, 2).Range.Text = strVersion

                    .Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

                    .Cell(1, 3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight

                    Set oCell = .Cell(1, 3).Range

                    With oCell

                        .End = .End - 1

                        .Text = "Page "

                        .Collapse wdCollapseEnd

                        .Fields.Add oCell, wdFieldPage, , False

                        .Start = oTable.Cell(1, 3).Range.End - 1

                        .Text = " of "

                        .Collapse wdCollapseEnd

                        .Fields.Add oCell, wdFieldNumPages, , False

                    End With

                    With .Rows(1)

                        With .Borders(wdBorderTop)

                            .LineStyle = Options.DefaultBorderLineStyle

                            .LineWidth = Options.DefaultBorderLineWidth

                            .Color = RGB(0, 0, 128)

                        End With

                        With .Borders(wdBorderLeft)

                            .LineStyle = Options.DefaultBorderLineStyle

                            .LineWidth = Options.DefaultBorderLineWidth

                            .Color = RGB(0, 0, 128)

                        End With

                        With .Borders(wdBorderBottom)

                            .LineStyle = Options.DefaultBorderLineStyle

                            .LineWidth = Options.DefaultBorderLineWidth

                            .Color = RGB(0, 0, 128)

                        End With

                        With .Borders(wdBorderRight)

                            .LineStyle = Options.DefaultBorderLineStyle

                            .LineWidth = Options.DefaultBorderLineWidth

                            .Color = RGB(0, 0, 128)

                        End With

                        With .Borders(wdBorderVertical)

                            .LineStyle = Options.DefaultBorderLineStyle

                            .LineWidth = Options.DefaultBorderLineWidth

                            .Color = RGB(0, 0, 128)

                        End With

                    End With

                End With

            End With

        End If

    Next oFooter

Was this answer helpful?

0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-07-28T14:36:19+00:00

    For the table:

    you would need something like:

    Dim oCell as Range 'additional variable declaration

        For Each oFooter In oSec.Footers

            If oFooter.Exists Then

                Set rngFooter = oFooter.Range

                With rngFooter

                    Set oTable = rngFooter.Tables.Add(rngFooter, 3, 3)

                    With oTable

                        .Range.Font.name = "Arial"

                        .Range.Font.Size = "10"

                        .Range.Font.Color = wdColorDarkBlue

                        .Cell(1, 1).Range.Text = strTitle

                        .Cell(1, 2).Range.Text = strVersion

                        .Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

                        Set oCell = .Cell(1, 3).Range

                        With oCell

                            .End = .End - 1

                            .Text = "Page "

                            .Collapse wdCollapseEnd

                            .Fields.Add oCell, wdFieldPage, , False

                            .Start = oTable.Cell(1, 3).Range.End - 1

                            .Text = " of "

                            .Collapse wdCollapseEnd

                            .Fields.Add oCell, wdFieldNumPages, , False

                        End With

                        .Cell(1, 3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight

                        .Cell(3, 1).Range.Text = strCopyright

                        .Cell(3, 3).Range.Text = strClassification

                        .Cell(3, 3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight

                        .Borders(wdBorderTop).LineStyle = wdLineStyleNone

                        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

                        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

                        .Borders(wdBorderRight).LineStyle = wdLineStyleNone

                        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

                        .Borders(wdBorderVertical).LineStyle = wdLineStyleNone

                        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

                        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

                        With .Rows(1)

                            With .Borders(wdBorderTop)

                                .LineStyle = Options.DefaultBorderLineStyle

                                .LineWidth = Options.DefaultBorderLineWidth

                                .Color = RGB(0, 0, 128)

                            End With

                            With .Borders(wdBorderLeft)

                                .LineStyle = Options.DefaultBorderLineStyle

                                .LineWidth = Options.DefaultBorderLineWidth

                                .Color = RGB(0, 0, 128)

                            End With

                            With .Borders(wdBorderBottom)

                                .LineStyle = Options.DefaultBorderLineStyle

                                .LineWidth = Options.DefaultBorderLineWidth

                                .Color = RGB(0, 0, 128)

                            End With

                            With .Borders(wdBorderRight)

                                .LineStyle = Options.DefaultBorderLineStyle

                                .LineWidth = Options.DefaultBorderLineWidth

                                .Color = RGB(0, 0, 128)

                            End With

                            With .Borders(wdBorderVertical)

                                .LineStyle = Options.DefaultBorderLineStyle

                                .LineWidth = Options.DefaultBorderLineWidth

                                .Color = RGB(0, 0, 128)

                            End With

                        End With

                    End With

                End With

            End If

        Next oFooter

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-07-28T13:46:52+00:00

    This partially achieves the answer and puts a border around the top row.

    However, I need to place the border around the three 'cells' (See illustration in original post)

    I think I may need a real table.

    Any help on this would be appreciated.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-07-28T13:16:30+00:00

    Frankly you might have been better with a real table, however:

    Change the line to

    .Paragraphs(1).Range.Borders.Enable = True

    Was this answer helpful?

    0 comments No comments
  4. Suzanne S Barnhill 277.5K Reputation points MVP Volunteer Moderator
    2014-07-28T12:54:05+00:00

    Could you instead create the footer as desired, save it as a building block, and use the code to insert that building block?

    Was this answer helpful?

    0 comments No comments