Link to home
Start Free TrialLog in
Avatar of Darkstriker69
Darkstriker69

asked on

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

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!
Avatar of Eric Fletcher
Eric Fletcher
Flag of Canada image

You can do this without a macro by using field codes.

In your document's footer, create the following:
User generated image
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:
User generated image
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 (/*).
ASKER CERTIFIED SOLUTION
Avatar of Darkstriker69
Darkstriker69

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.)