Print a file's path in an Excel header or footer


Q I can't figure out how to print an Excel workbook's complete path in the header or footer. I can add the file's path manually, but if I move a file I'd like the path to be updated automatically on future printouts. Am I missing something?

û Ron Daniel

A This is one of Excel's most requested features. The solution is a macro. If you would prefer not to create a macro yourself, you can try an Excel 97 add-in that I developed. The Add Path add-in asks you where you want the path printed, then inserts the appropriate macro code for you. You will find Add Path on www.j-walk.com/ss and on our cover CD.

Caption: The Add Path add-in displays a workbook's complete path in the page header or footer

If you like to create macros, follow these steps in Excel 97:

1. Press <Alt>-<F11> to activate the Visual Basic Editor.

2. Locate your workbook in the Project window and double-click its file name.

3. Double-click ThisWorkbook to display the code module for the workbook object.

4. Enter the following subroutine into the code module ThisWorkbook:

Private Sub Workbook_BeforePrint(Cancel As Boolean)

ActiveSheet.PageSetup.LeftHeader = ActiveWorkbook.FullName

End Sub

This subroutine takes advantage of a "before print" event in Excel 97. Before you print or preview any sheet in the workbook, the macro inserts the workbook's full path into the left header. To put the path in a different page location, substitute any of the following for LeftHeader: CenterHeader, RightHeader, LeftFooter, CenterFooter or RightFooter. (Note: this subroutine will overwrite any existing information in the specified header or footer.)

In earlier versions of Excel, you need to take a different approach:

1. Select InsertûModule to insert a new Visual Basic module.

2. Enter the following subroutine:

Sub UpdatePath()

For Each sht In ActiveWorkbook.Sheets

sht.PageSetup.LeftHeader = ActiveWorkbook.FullName

Next sht

End Sub

û John Walkenbach


Category:Spreadsheet
Issue: September 1998

These Web pages are produced by Australian PC World © 1998 IDG Communications