none
How to setup Left/Right margin for header/footer? RRS feed

  • Question

  • Although Document.PageSetup.LeftMargin/RightMargin will set header/footer margin initially, the header/footer margin may change after I am inserting word file into my document.  Tried to reset the Document.PageSetup.LeftMargin/RightMargin did not work.  Try to set Range.PageSetup.LeftMargin/RightMargin where Range is from Section.Range may cause exception "out of range".  I am totally stuck with this issue.

    Regards,

    Kelvin

    Tuesday, March 28, 2017 5:03 PM

Answers

  • Hello Kelvin,

    What code are you using now?

    To re-set the margin of Header/Footer, we could use Paragraphformat.LeftIndent/RightIndent

    Here is the example.

        Dim rng As Range
        Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
        With rng.ParagraphFormat
            .LeftIndent = InchesToPoints(2)
            .RightIndent = InchesToPoints(1)
        End With

    Besides, you could use the following code to set margin for Section:

    With ActiveDocument.Sections(1).PageSetup
    .LeftMargin = InchesToPoints(1)
    .RightMargin = InchesToPoints(0.5)
    End With

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kelvin Jin Thursday, April 6, 2017 1:57 PM
    Wednesday, March 29, 2017 10:14 AM
    Moderator

All replies

  • Hello Kelvin,

    What code are you using now?

    To re-set the margin of Header/Footer, we could use Paragraphformat.LeftIndent/RightIndent

    Here is the example.

        Dim rng As Range
        Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
        With rng.ParagraphFormat
            .LeftIndent = InchesToPoints(2)
            .RightIndent = InchesToPoints(1)
        End With

    Besides, you could use the following code to set margin for Section:

    With ActiveDocument.Sections(1).PageSetup
    .LeftMargin = InchesToPoints(1)
    .RightMargin = InchesToPoints(0.5)
    End With

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kelvin Jin Thursday, April 6, 2017 1:57 PM
    Wednesday, March 29, 2017 10:14 AM
    Moderator
  • Hi Celeste,

    Thanks for the reply.

    I have been using Sections.Last to grab the last section and then assign parameters for the footer in that Section, but often throw to exception that "value out of range".  After much of dig, I found the section number could go above existing number if I insert a blank page as a file.  I then trace back the previous section if that special situation happens which solved the problem.

    Again, thanks for the reply.

    Kelvin

    Friday, March 31, 2017 3:49 PM
  • Hi Kelvin,

    Thanks for sharing the solution.

    I would suggest you mark your solution as answer, and then others who run into the same issue would find the solution easily.


    Help each other

    Monday, April 3, 2017 8:28 AM