• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Custom field in word 2010 template - how to?

In a small network with a handful of users but some sharing of PC's there is now a requirement to show the logged in user as the creator of a word document based on a centrally located template.
So it would be nice to have a field in the template (eg. in the footer) which will get the login name of the user when the template is used to create a new document.  There is a tiny problem, the login names are in the format of John.Smith or Harry.Brown so the '.' needs to also be replaced with a space.

I can insert a field, what do I need to do to get the login name AND then replace all instances of '.' with a space in that retrieved name to put into that field at the time of document creation.  (I don't want this information changing should another user edit the document at a later time.

(In theory one could go to each PC, login as each potential user and change a setting in the options for word - but that is a horrible looking hack and waste of time and resources)
  • 3
2 Solutions
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
I don't believe you can directly. You can do some wonderful tricks with fields, but they seem to be mostly used with numbers - see http://www.techrepublic.com/blog/10-things/10-power-tricks-for-using-word-field-switches/ as an example.

What you can do instead is to change what the computer thinks the original author is at the time of document creation.

You would create your field in Normal.dot relating to the Author (Insert - Quick Parts - Fields - Author - OK), and then have this code in Normal.dot (press Alt+F11, and go to Normal - Microsoft Word Objects - This Document):

Private Sub Document_New()
Me.BuiltInDocumentProperties("Author") = Replace(Me.BuiltInDocumentProperties("Author"), ".", " ")
End Sub

Open in new window

You would then need a certificate to verify your identity on other computers - you can use SelfCert.exe if you haven't go one - and then associate that certificate with your code (Tools - Digital Certificate).

It all depends on whether you are used to using VBA or not as to whether I would recommend going down that route.
You can use this function to find the log-in name,
Function GetLoginName() As String
    Dim i As Integer
    Dim strParts() As String
    i = 1
    Do Until Environ(i) = Empty
        strParts = Split(Environ(i), "=")
        If StrComp(strParts(0), "USERNAME", vbTextCompare) = 0 Then
            GetLoginName = Replace(strParts(1), ".", " ")
        End If
        i = i + 1
End Function

Open in new window

which you can use to set the Author property.
Private Sub Document_New()
      ActiveDocument.BuiltInDocumentProperties("Author") = GetLoginName()
End Sub

Open in new window

AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Thanks for the responses.  Hmm, not as trivial as I expected.  I'll see if I can get it tested today.
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
I've got it to work here on a single PC, it has been passed on for testing (by my tame tester) in situ in the real life network.  I'll close this / ask for further advice when I get a response back.

ps.  Please cast a glance at:
It is a follow up to this question.
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Great work guys, your help is appreciated.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now