How Can I Put the User Name into the Footer of a Microsoft Word Document?

How Can I Put the User Name into the Footer of a Microsoft Word Document?

  • Comments 3
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I put the user name into the footer of a Microsoft Word document?

-- RR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RR. That’s an interesting question, and probably the first how-do-I-put-the-user-name-in-the-footer-of-a-Word-document question we’ve ever received. (No, really!) But what the heck; let’s see if we can find an answer for you. Besides, this gives us a chance to shamelessly plug the upcoming Scripting Week 2 webcast series. Among the 10 webcasts scheduled for that week: If You Want Something Done Right, Let Microsoft Office Do It For You, a webcast that will provide an introduction to scripting Microsoft Office applications. If you’re interested in scripting Word, Excel, and/or PowerPoint, this might be 90 minutes well spent.

Of course, right now all you want to do is get the user name into the footer of a Word document. No problem; in fact, we’ll show you two different ways to do this.

As the Microsoft Scripting Guys, we spend most of our time helping system administrators write VBScript scripts. Because of that, let’s first show you a way to do carry out this task from a .vbs file. The following script determines the name of the logged-on user, starts an instance of Word, creates a new document, and then sets the document footer text to the name of the logged-on user:

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

objDoc.Sections(1).Footers(1).Range.Text = strUser
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1

What, you expected big, long, and complicated? Actually, scripting Microsoft Word is pretty easy, as this particular sample demonstrates. In this script, the first two lines of code use the WSH Network object to grab the name of the logged-on user and then store that name in the variable strUser. The next three lines of code create an instance of Microsoft Word, make that instance visible on screen, and then give us a blank document to work with.

At that point, we need only one line of code to set the footer text:

objDoc.Sections(1).Footers(1).Range.Text = strUser

To clarify a bit, footers are tied to document sections; because this document has only a single section, we specify Sections(1). Footers(1), which means we want to add the text to the primary footer in the section, while; Range.Text indicates that we want to set the Text property of the footer. And strUser, of course, is the name of the logged-on user. That’s it; that’s all it takes.

Just for the heck of it, though, we did add a final line of code that centers the footer on the page. Hey, why not?

Of course, it’s quite possible that you don’t want to perform this trick from a .vbs file; instead, you’d like to have this code available as a Word macro. In that case, use a macro script similar to this:

Sub FooterAdder()

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName

ThisDocument.Sections(1).Footers(1).Range.Text = strUser
ThisDocument.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1
End Sub

Here we have a subroutine named FooterAdder that gets the name of the logged-on user and makes that the document footer. Notice that we don’t need to create an instance of Word; if you’re running a Word macro, we’re gonna assume that Word is already running. In addition, this code doesn’t create a new document; instead, it simply adds the footer to the current document. That’s what ThisDocument indicates.

Incidentally, we know there is a lot of interest in scripting Office applications, and - hopefully - that interest will only increase after Scripting Week 2. So keep an eye on the Script Center during the month of February. Sometime during that month, we plan to unveil a new section (Office Space) devoted to scripting Microsoft Office. In the meantime, let us know what other Office-related questions you might have. We’ll try to answer a few of those between now and the debut of Office Space.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Is it possible to to the same with a PDF file? With a twist, e.g. add file name in footer and print the PDF, leaving the original unchanged? Now, in PDF JavaScript rules. This will add a footer (watermark):

    this.addWatermarkFromText({cText: this.documentFileName, nFontSize: 8, nTextAlign: app.constants.align.center, nHorizAlign: app.constants.align.center, nVertAlign: app.constants.align.bottom, nVertValue: 5 });

    What I don't get is how to run this like you did with Word. To difficult?

    Regards, Dag

  • What if I want the username to be inserted in another choosen place, as in the middle of an sentance?

    How can I do this? Can anyone point me to an quick traing guide or something?

  • In all cases we use the selection object and range object to place text, pictures, hyperlinks anywhere in a document.

    All of this is documented in teh Word VBA help file which needs to be installed from the CD.

    See the Office Developer Forums for more help.

    social.msdn.microsoft.com/.../officedev