Share via

VBA code for header on every page

Anonymous
2017-07-30T17:29:27+00:00

Need a macro which will

  1. delete the header (side image on page 2)
  2. Insert a new image on page 2 that will show up on every page (including page 1)

I can do this manually but can't seem to get the code to do it in a macro.

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

6 answers

Sort by: Most helpful
  1. Anonymous
    2017-07-31T14:39:06+00:00

    No section breaks.  Want to place the jpg image in header on first page halfway down left hand side (it's a court brief with ruled and numbered paper).  Once it's on the first page, it will be on ALL following pages

    Was this answer helpful?

    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2017-07-31T14:26:07+00:00

    > No, "different first page" is NOT checked.

    So, is there a section break between pages 1 and 2? That situation would require completely different code.

    > the side image is about halfway down the page on the left hand side

    That's why the code I posted saves the current image's .Left and .Top properties and applies the same values to the new image. That part will have to be in whatever final code we come up with.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-07-31T02:51:01+00:00

    Also, bear in mind the side image is about halfway down the page on the left hand side, not at the top of the document

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-07-31T02:41:22+00:00

    Thank you Jay.  No, "different first page" is NOT checked.

    Was this answer helpful?

    0 comments No comments
  5. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2017-07-31T02:37:17+00:00

    This question can't be answered for certain unless you can tell us what settings in the original document cause the header on page 1 to be different from the header on page 2.

    • There could be one section (no section break) with the Different First Page option turned on in the Header & Footer Tools ribbon.
    • Or there could be two sections, with a section break between pages 1 and 2, with or without the Different First Page option.

    Since the first one is more likely, here's some code to try. You'll have to supply the path of the new image's file.

    Sub ChangeHeaders()

        Dim rg As Range

        Dim pic As Shape

        Dim lngTop As Long

        Dim lngLeft As Long

        Dim lngWidth As Long

        With ActiveDocument.Sections(1)

            .PageSetup.DifferentFirstPageHeaderFooter = False

            With .Headers(wdHeaderFooterPrimary).Shapes(1)

                Set rg = .Anchor

                lngTop = .Top

                lngLeft = .Left

                lngWidth = .Width

                .Delete

            End With

            Set pic = .Headers(wdHeaderFooterPrimary).Shapes.AddPicture( _

                FileName:="D:\Pictures\XYZ.jpg", _

                Left:=lngLeft, Top:=lngTop, Anchor:=rg)

            With pic

                .LockAspectRatio = msoTrue

                .Width = lngWidth

            End With

        End With

    End Sub

    Was this answer helpful?

    0 comments No comments