Insert the Current Windows Login Name in a Word Document
Question details
Insert the current Windows login name into a Word document and update it when the document opens or is created.

- Product
- Microsoft Word
- Device & OS
- Windows
- Scenario
- A document needs to show the login name of the Windows user rather than the document author.
- Observed behavior
- A DocVariable field displays the login name most recently stored by a macro.
Use desktop Word on Windows and check that macros are permitted by your organization's security settings. Save the document as a macro-enabled Word document (.docm), or use a macro-enabled template (.dotm) for new documents.
Display the Windows login name with a DocVariable field
Use this method to store the Windows USERNAME value in the document and display it when the macro runs.
The field shows the most recently stored value; it does not independently read the Windows login name. The Word Author field is not a substitute because it identifies the document author.
Press Alt+F11 to open the Visual Basic editor. Select the document's project, choose Insert > Module, and paste: Sub AutoNew() GetUserName End Sub Sub AutoOpen() GetUserName End Sub Sub GetUserName() Dim loginVar As Variable On Error Resume Next Set loginVar = ActiveDocument.Variables("username") On Error GoTo 0 If loginVar Is Nothing Then ActiveDocument.Variables.Add Name:="username", Value:=Environ("USERNAME") Else loginVar.Value = Environ("USERNAME") End If ActiveDocument.Fields.Update End Sub
Return to Word, press Alt+F8, select GetUserName, and click Run. This stores the current Windows USERNAME value in the document.
Place the cursor in the document body. Choose Insert > Quick Parts > Field, select DocVariable, enter username as the variable name, and click OK.
Choose File > Save As and save as a Word Macro-Enabled Document (.docm). Close and reopen it, enable macros only if you trust the file, and check the displayed name. For new documents, put the macro and field in a macro-enabled template (.dotm) and create documents from that template.

Edit Word Documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for editing Word documents. Its format support makes it easy to move everyday documents from Microsoft Office, though it does not fix Word's macro behavior.

Frequently Asked Questions
Why does the field show an old login name?
The field displays the value last stored in the document. In desktop Word, press Alt+F8, select GetUserName, and click Run, or reopen the document with trusted macros enabled.
Can I use the Author field for the current Windows user?
No. The Author field identifies the document author, not necessarily the person logged in to Windows. Use a DocVariable field populated from Environ("USERNAME") instead.
How can I see or update the field code?
Press Alt+F9 in Word to toggle field codes. The code is { DOCVARIABLE username }. Select the field and press F9 to update its display from the stored variable.
Will the macro work in a DOCX file?
A DOCX file cannot retain VBA macros. Use File > Save As to save the document as a Word Macro-Enabled Document (.docm), or save a template containing the macro as a .dotm file.




