VBA Macro for Footer on only the last page of a document

Darkstriker69
Darkstriker69 used Ask the Experts™
on
Hello,

Using Word 2010.  Need code for inserting the document filename and path (c:\docs\document1.doc) on the left side and date and time on the other side of a footer but only on the last page of a document.  Using 8-pt. Times New Roman.

Goal is to have a toolbar button for users. I know nothing about VBA.

I would like a solution simmilar to this post but with the addition of the date and time right justified:

https://www.experts-exchange.com/questions/22048617/VBA-macro-for-simple-last-page-footer.html

 
Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can do this without a macro by using field codes.

In your document's footer, create the following:
Field code formula to set a footer on ONLY the final page
The formula will be interpreted as "if the current page number is equal to the total number of pages in the document, display the filename with its path, then a tab, then the current date and time; otherwise, display nothing". When Word builds the page, the result of the formula will manage what is displayed in the footer -- and you'll only see content on the final page. Here's what the footers looks like for the final 2 pages of the sample document:
Footer showing only on the final page
To enter the field codes, you can type it in (as I've done above) by pressing Ctrl-F9 for each field code to get the curly braces; you cannot type them in. You can also use the Insert > Quick Parts > Fields... dialog (the ribbon wording may be different for Word 2010).

Either way, use Alt-F9 to toggle between the view of the result (which will be blank unless you happen to be on the final page) and the field codes (as shown above).

Note that this method allows you to use formatting to set the content in whatever font style you need.  Also, to display the date and time using a different format, change the number format in the "TIME" field code. The dialog box offers a selection of different formats, but you can customize it by reading about field code formatting switches (/*).
Thanks,

I needed a macro to add the field codes to a document then prints the documents (only because adding the field codes to every existing document is very complicated to type) I ended up editing the post that I linked with some guess and check until it worked. Here is the code if it helps anybody.

Don't ask me how or why it works, lol:

Sub LastPageField()
    Dim rng As Range
    Dim ftr As HeaderFooter
    Dim fld As Field
    Dim fld1 As Field
    Dim fld2 As Field
    Dim fld3 As Field
    Dim fld4 As Field
    Dim fld5 As Field
    Dim f As Integer
    For Each ftr In ActiveDocument.Sections.Last.Footers
       If ftr.Exists Then
            If ftr.Range.Fields.Count > 3 Then
                If ftr.Range.Fields(1).Type = wdFieldIf Then
                    ftr.Range.Fields.Update
                    Exit Sub
                End If
            End If
            Set rng = ftr.Range
            rng.Collapse wdCollapseStart
            rng.ParagraphFormat.Alignment = wdAlignParagraphLeft
            Set fld = ActiveDocument.Fields.Add(rng, wdFieldIf, "  \* CHARFORMAT", False)
            fld.ShowCodes = True
            rng.MoveEnd wdCharacter, 5
            rng.Font.Name = "Times New Roman"
            rng.Font.Size = 8
            rng.Collapse wdCollapseEnd
            Set fld1 = ActiveDocument.Fields.Add(rng, wdFieldPage, , False)
            fld1.ShowCodes = True
            rng.Move wdCharacter, 9
            rng.InsertAfter "="
            rng.Move wdCharacter, 2
            Set fld1 = ActiveDocument.Fields.Add(rng, wdFieldNumPages, , False)
            fld1.ShowCodes = True
            rng.Move wdCharacter, 12
            Set fld1 = ActiveDocument.Fields.Add(rng, wdFieldFileName, " \p", False)
            rng.MoveEnd wdCharacter, 24
            rng.InsertAfter vbTab
            rng.InsertAfter vbTab
            rng.Collapse wdCollapseEnd
            Set fld2 = ActiveDocument.Fields.Add(rng, wdFieldIf, "  \* CHARFORMAT", False)
            fld2.ShowCodes = True
            rng.MoveEnd wdCharacter, 5
            rng.Font.Name = "Times New Roman"
            rng.Font.Size = 8
            rng.Collapse wdCollapseEnd
            Set fld3 = ActiveDocument.Fields.Add(rng, wdFieldPage, , False)
            fld3.ShowCodes = True
            rng.Move wdCharacter, 9
            rng.InsertAfter "="
            rng.Move wdCharacter, 2
            Set fld3 = ActiveDocument.Fields.Add(rng, wdFieldNumPages, , False)
            fld3.ShowCodes = True
            rng.Move wdCharacter, 12
            Set fld3 = ActiveDocument.Fields.Add(rng, wdFieldDate, , False)
            rng.Move wdCharacter, 24
            rng.InsertAfter " "
            rng.Collapse wdCollapseEnd
            Set fld4 = ActiveDocument.Fields.Add(rng, wdFieldIf, "  \* CHARFORMAT", False)
            fld4.ShowCodes = True
            rng.MoveEnd wdCharacter, 5
            rng.Font.Name = "Times New Roman"
            rng.Font.Size = 8
            rng.Collapse wdCollapseEnd
            Set fld5 = ActiveDocument.Fields.Add(rng, wdFieldPage, , False)
            fld5.ShowCodes = True
            rng.Move wdCharacter, 9
            rng.InsertAfter "="
            rng.Move wdCharacter, 2
            Set fld5 = ActiveDocument.Fields.Add(rng, wdFieldNumPages, , False)
            fld5.ShowCodes = True
            rng.Move wdCharacter, 12
            Set fld5 = ActiveDocument.Fields.Add(rng, wdFieldTime, , False)
            fld.Update
            fld2.Update
            fld4.Update
        End If
    Next ftr
    ActiveDocument.PrintOut Copies:=1
End Sub
I didn't examine your code in detail, but if it works for you, great! And if you really didn't know anything about VBA, you've apparently learned a lot remarkably quickly because that is a complex bit of code!

A few points though...

1. Your code doesn't save the document after making the changes you want to print. If you want the footer to be there permanently, you may want to add a line to save it.

2. You could probably achieve the same ends by creating a building block (or formatted AutoText) entry with the full-formatted footer, and then just insert it via VBA as the final section's footer.

3. If you don't intend to save the document — i.e. if you are doing it just for printing a reference copy — then modifying a document's footer may be risky and could too easily mess up a footer that was deliberately created as an integral part of the document. As an alternative that would not alter any intentional footers, consider creating a building bloc consisting of the content you need contained within a floating text box. Your VBA code could then insert it at the end of the document so it would appear on the final page without affecting the footers at all. (If you used this approach, be sure to use the Layout dialog to set the text box to float “above” the page content plus turn off the “Move with text” option and set a specific location so it could appear consistently in the same position on the last pages.)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial