none
Word document capable of picking up Exchange User Details? RRS feed

  • Question

  • Hi Everyone,

    Ive been given the task of creating a template word document. The word document should automatically pick up the Users full name, Job Title, Telephone Number and Email Address.

    Im able to gain the User name by inseting the Author Document Property, this works fine. However most or all of the other properties are left blank.

    After a lot of searching the web, ive come to the conclusion the only way to make this work is to write some vba to retrieve the details from the users exchange mailbox using the ExchangeUser object property. I could then try to populate the document properties with the correct info.

    Im trying to write a macro in Word2010 but cant seem to correctly reference the ExchangeUser Object. I tried using the example below found on the msdn reference site for the exchangeuser object. However as soon as I run the macro i get user defined type not defined error on the first line.

     

    Dim colAL As Outlook.Addresslists
     Dim oAL As Outlook.Addresslist
     Dim colAE As Outlook.AddressEntries
     Dim oAE As Outlook.AddressEntry
     Dim oExUser As Outlook.ExchangeUser
     Set colAL = Application.Session.Addresslists
     For Each oAL In colAL
     'Address list is an Exchange Global Address List
     If oAL.AddressListType = olExchangeGlobalAddressList Then
     Set colAE = oAL.AddressEntries
     For Each oAE In colAE
     If oAE.AddressEntryUserType = _

     olExchangeUserAddressEntry Then
     Set oExUser = oAE.GetExchangeUser
     Debug.Print (oExUser.JobTitle)
     Debug.Print (oExUser.OfficeLocation)
     Debug.Print (oExUser.BusinessTelephoneNumber)
     End If
     Next
     End If

    Any help on why this supposed working example is not working would be greatly appreciated.

    Tuesday, March 20, 2012 4:15 PM

Answers

All replies

  • Firstly, you need to add Outlook to your project references.

    Secondly, why do you loop through all address entries? Why not use Application.Session.CurrentUser?

    Also, since you are running Word VBA, Application intrinsic variable will point to the Word.Application object, not Outlook.Application.

    Do not use "Application" name for the variable and initialize it using CreateObject():

    set OlApp = CreateObject("Outlook.Application")


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Tuesday, March 20, 2012 4:44 PM
  • Hi,

    as far as I know,

    you can get the Info via Active Directory, too:

    'On Error Resume Next
    Dim qQuery As String
    Dim objSysInfo As Object
    Dim objUser As Object
    Dim FullName, EMail, PhoneNumber, FaxNumber
    
    ' Active Directory Informationen für den angemeldeten User lesen
    Set objSysInfo = CreateObject("ADSystemInfo")
    qQuery = "LDAP://" & objSysInfo.UserName
    Set objUser = GetObject(qQuery)
    
    'Variablen mit AD-Attributen füllen (es gibt viel mehr Attributen zb. FaxNumber usv.)
    FullName = objUser.FirstName & " " & objUser.lastname
    EMail = objUser.mail
    PhoneNumber = objUser.telephoneNumber
    FaxNumber = objUser.facsimileTelephoneNumber
    
    'Vordefinierte Textmarken in Word füllen
    'ActiveDocument.Bookmarks("txtName").Range.Text = FullName
    'ActiveDocument.Bookmarks("txtEmail").Range.Text = EMail
    'ActiveDocument.Bookmarks("txtTel").Range.Text = PhoneNumber
    'ActiveDocument.Bookmarks("txtFax").Range.Text = FaxNumber
    taken from:

    http://www.office-loesung.de/ftopic143488_0_0_asc.php

    Hope this helps,

    greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]

    Wednesday, March 21, 2012 6:57 AM
    Answerer
  • Hi All,

    Thanks for the replies. In answer to some of your questions...

    - I have the Microsoft Office 14.0 Object Library referenced. As far as im aware this is what I need to make the outlook objects available in VBA.

    - The code I pasted above was taken from an example given on the MSDN website for the ExchangeUser object. I dont intend on using the Loop, I was just trying to run the example code.

    Before I posted yesterday I was trying to start out with the following code. However Im met with an error saying Object doesnt support this property or method. This is referring to the Set ExObj = OlObj.ExchangeUser line of code.

       Dim OlObj As Object
       Dim ExObj As Object
       Dim FullName, JobTitle, DirNo, MobNo
       
       
       Set OlObj = CreateObject("Outlook.Application")
       Set ExObj = OlObj.ExchangeUser

    I will try Helmut's code and see what happens.

    Thanks again for all your help.

    Regards

    Tom

    Wednesday, March 21, 2012 9:54 AM
  • Application object in Outlook does bot expose the ExchangeUser property. Use

    Application.Session.CurrentUser.AddressEntry.GetExchangeUser


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Friday, March 23, 2012 3:50 AM