by Allen Wyatt
(last updated November 22, 2014)
Vladimir knows that he can put a document's filename into the header of a page. What he would like, though, is to only have the last five characters of the filename (exclusive of the filename extension) appear in the header. He is casting about for the best way to accomplish the task.
The only way to do this is by using a macro; there is no built-in functionality in Word to accomplish the task. The macro needs to determine the filename, grab the characters desired, and then stuff then into the header. There are potential complications with something that might seem so simple, though. For instance, what if the document hasn't been saved and therefore has no filename yet? What if the document has been saved, but there are fewer than five characters in the filename? What should the macro do if there is already something in the header? What should it do if the user in looking at the document in a view that doesn't display headers?
To deal with such questions, the macro needs to make some assumptions. For this example, we'll assume that the macro should simply replace whatever existing header there is with the desired portion of the filename. Further, the macro can switch the viewing mode to Print Layout view so the header is easy to work with. Here's the result:
Sub PartFilenameInHeader() Dim sName As String Dim J As Long sName = ActiveDocument.Name J = InStrRev(sName, ".") If J > 0 Then sName = Left(sName, J - 1) If Len(sName) > 5 Then sName = Right(sName, 5) End If If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader Selection.WholeStory Selection.Delete Selection.TypeText Text:=sName ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Else MsgBox "Document has no filename extension." End If End Sub
The macro first checks to ensure that the document has a real filename (from the .Name property of the ActiveDocument object). If so, then it pulls five (or fewer) characters from the filename. It checks to make sure there are not multiple panes open and that the document is in Print Layout view. It then selects whatever is currently in the header and replaces it with the desired characters from the filename.
Understand that the macro should be run once after saving the document and once again if you ever save the document using a different filename.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13316) applies to Microsoft Word 2010 and 2013.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Headers and footers add a nice touch to your documents, particularly if they are printed. You may want Word to use a ...Discover More
Don't want a header or footer to appear on just a portion of your document? It's easy to do when you understand that ...Discover More
Editing what is in your page header or footer is fairly easy, and you can use the same editing techniques you already ...Discover More
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.