Share via

VBA code for adding line to Header

Anonymous
2016-08-15T03:51:17+00:00

Hello,

I’m learning VBA and having trouble figuring out how to place a line which extends beyond the margins in the header/footer. 

I have this code which works to insert a line formatted how I want it –

Set shp = ActiveDocument.Shapes.AddLine(10, 100, 590, 100)

 With shp.line

      .DashStyle = msoLineSolid

      .ForeColor.RGB = RGB(0, 0, 139)

      .Weight = 2.5

 End With

I can’t figure out how to get this in the header.

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
2016-08-15T05:46:52+00:00

As I indicated, the code I posted adds the line to ALL the header ranges in the document.

If you want to add it to just the first page header then

Dim shp As Shape

Dim oRng As Range

Dim oHeader As HeaderFooter

Dim oSection As Section

    Set oSection = ActiveDocument.Sections(1)

    oSection.PageSetup.DifferentFirstPageHeaderFooter = True

    Set oHeader = oSection.Headers(wdHeaderFooterFirstPage)

    Set oRng = oHeader.Range

    Set shp = ActiveDocument.Shapes.AddLine(10, 100, 590, 100, oRng)

    With shp.Line

        .DashStyle = msoLineSolid

        .ForeColor.RGB = RGB(0, 0, 139)

        .Weight = 2.5

    End With

You can do it in fewer lines

Dim shp As Shape

    ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True

    Set shp = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.AddLine(10, 100, 590, 100)

    With shp.Line

        .DashStyle = msoLineSolid

        .ForeColor.RGB = RGB(0, 0, 139)

        .Weight = 2.5

    End With

When working with ranges, there is no need to select the range in order to process it, but you must set the layout option to use a different first page.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-08-15T06:14:04+00:00

    Thanks so much Graham, that was extremely helpful. I really appreciate it.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-08-15T05:10:26+00:00

    Thanks very much for your reply. I wanted to put it in First Page header only. I used your code and it worked perfectly except it also created a header on the 2^nd^ page.

    Unfortunately, I don’t understand what you’ve done so don’t know how to adjust that to make it first page only.

    I’ve been trying to figure this out for days and was thinking it should be possible to do something like change the Set line so it included the Header and got to this point –

    Set shp = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)Shapes.AddLine(10,100,590,100)

    This doesn’t work. It doesn’t give an error code but nothing happens.

    Would that have been a possibility or am I totally off track?

    I also tried to do it this way which adds the line but not in the Header -

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

    Set shp = ActiveDocument.Shapes.AddLine(10, 100, 590, 100)

     With shp.line

          .DashStyle = msoLineSolid

          .ForeColor.RGB = RGB(0, 0, 139)

          .Weight = 2.5

     End With

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-08-15T04:25:19+00:00

    Your question does not indicate which of the many possible headers you wish to insert this line. There are potentially three headers for each section of the document (first page, primary, even pages). You can specify which header you wish to process and set a range to that header, then anchor the shape to the range. The following will put the line in any header that exists in the document

    Dim shp As Shape

    Dim oRng As Range

    Dim oHeader As HeaderFooter

    Dim oSection As Section

        For Each oSection In ActiveDocument.Sections

            For Each oHeader In oSection.Headers

                If oHeader.Exists Then

                    Set oRng = oHeader.Range

                    Set shp = ActiveDocument.Shapes.AddLine(10, 100, 590, 100, oRng)

                    With shp.Line

                        .DashStyle = msoLineSolid

                        .ForeColor.RGB = RGB(0, 0, 139)

                        .Weight = 2.5

                    End With

                End If

            Next oHeader

        Next oSection

    Was this answer helpful?

    0 comments No comments