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)
LVL 46
AndyAinscowFreelance programmer / ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
GrahamSkanRetiredCommented:
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
    Loop
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
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:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_28523653.html
It is a follow up to this question.
0
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Great work guys, your help is appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.