Footer of a Word document RRS feed

  • Question

  • Hello,

    I am working on the footer of a Word document (from VBA Excel).

    My problem is the page number is too close to the horizontal line.

    What is the solution to have an empty line between the horizontal line and the page number ?

    Here is my code :

    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Set WordApp = CreateObject("Word.Application")
        WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Add
    With WordDoc.Sections(1)
            .Footers(wdHeaderFooterPrimary).Range.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
    	.Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter
            .Footers(wdHeaderFooterPrimary).Range.Font.Name = "Courier New"
            .Footers(wdHeaderFooterPrimary).Range.Font.Size = 9
    End With
    End Sub

    Thank you very much for your help.

    Wednesday, October 7, 2015 12:46 PM


  • Hi vba_sg

    You don't mention which version of Word you're using, so it's not certain that your code yields the same result I'm seeing in Word 2010... However, I don't recommend you use this method to insert a page number as it "floats" the number over the text layer rather than embedding it in the text.

    In Word 2010 it "floats" in a FRAME object, so you'd need to change the vertical position of the Frame. The Add method does not return the Frame, it returns a PageNumber object and this has no direct way to address the Frame. This means your code would need to SELECT the PageNumber and through the Selection object you can probably access the Frame: Selection.Frames(1)

    The VerticalPosition property of the Frame object lets you set the distance of the Frame relative to its anchor point, which by default will be the Footer paragraph. Note that the setting is measured in POINTS.

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 7, 2015 2:50 PM