I was working for a company with offices all over Europe and they wanted to have a word template that included the Active Directory data (name, title, street address etc) when a new document was created.
This is how I did it… Sorry for using Word 2003, but that was what they were running at the time.
Open up word and create a new file and save it as a .dot template. For me it looked something like this with a header and a footer.
Go to Tools -> Macro -> Macros (Alt+F8). Choose a name called AutoNew and press Create. It has to be AutoNew because then it will automatically be run when a new document is created [Reference].
Now cut’n’paste the following Macro. It will get the date and the following Active Directory attributes:
- givenName
- sn
- title
- streetAddress
- postalCode
- l (city)
Option Explicit Sub AutoNew() With ActiveDocument.Bookmarks("MyDate").Range .InsertBefore Format(Date, "dd mmmm yyyy") End With With ActiveDocument.Bookmarks("MygivenName").Range .InsertBefore GetgivenName End With With ActiveDocument.Bookmarks("Mysn").Range .InsertBefore Getsn End With With ActiveDocument.Bookmarks("MyTitle").Range .InsertBefore GetTitle End With With ActiveDocument.Bookmarks("MystreetAddress").Range .InsertBefore GetstreetAddress End With With ActiveDocument.Bookmarks("MypostalCode").Range .InsertBefore GetpostalCode End With With ActiveDocument.Bookmarks("Myl").Range .InsertBefore Getl End With End Sub Function GetgivenName() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) GetgivenName = objUser.get("givenName") End Function Function Getsn() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) Getsn = objUser.get("sn") End Function Function GetTitle() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) GetTitle = objUser.get("Title") End Function Function GetstreetAddress() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) GetstreetAddress = objUser.get("StreetAddress") End Function Function GetpostalCode() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) GetpostalCode = objUser.get("postalCode") End Function Function Getl() As String Dim objSysinfo As Object Dim objUser As Object Dim strUser As String 'Distinguished Name Set objSysinfo = CreateObject("ADSystemInfo") strUser = objSysinfo.UserName Set objUser = GetObject("LDAP://" & strUser) Getl = objUser.get("l")
End Function
It uses bookmarks (more explained here where I got this idea) which means that the document will contain bookmarks and this is where it will import the data it gets from Active Directory.
Save/close the Macro and go back to the template.
At the specific fields <date>, <firstname>, <lastname> etc, remove this variable and instead, choose Insert -> Bookmark. Put the bookmark variables above (beginning with My*, also marked in red). You won’t see anything in the document but this is where the template will insert the data.
Save the template and create a new document using this template. It should get the data from Active Directory.
Please comment if you find this article useful.
Worked like a charm in Word 2007.
I only got tricked by the Letter “L” where city was located, but otherwise it only took a couple of minutes to get it all working.
My goal was to create global fax, letter, meeting minutes,and memo templates from a shared template directory which populate from A/D. At this rate, I may be looking for something else to do for the rest of the day!
Thanks!
This Works great. However, if I decide to use the normal.dot template to generate the user information. It will not work if Outlook is open.
Thanks for the good manual!
Is it possible to skip one field if the acvive directory field is empty? Acutal there is a runtime error if the AD field is empty.
Tranks
It is very easy, with this Tool: ldap2doc.de
Get the Free Trial for two Users.
Kind Regards
Franze Mayer
Hello,
wie have found a very easy to use Word ADD-IN.
Try it out: http://www.ldap2doc.de
Kind Regards
Franze Mayer