BobEulberg
Programmer
I'm trying to retrieve info on the user of whatever machine is running my code. From support.microsoft.com ticket 179083 I found the code at the far bottom (I trimmed 20 or so extra fields in the For loop to save space). But I need to use this code in Reflections (or any program other than Outlook for that matter).
Simply put, I thought I could throw Outlook.Application in front of everything. That was what I attempted in my code below. But on this line I get Run-time error '13': Type mismatch.
Set objSession = olApp.CreateObject("MAPI.session")
I'm new to objects and using them to control other programs, I'm pretty sure that's how I messed this up. Could anyone out there help me out?
Thanks for your time! Bob
My code:
support.microsoft.com code:
Simply put, I thought I could throw Outlook.Application in front of everything. That was what I attempted in my code below. But on this line I get Run-time error '13': Type mismatch.
Set objSession = olApp.CreateObject("MAPI.session")
I'm new to objects and using them to control other programs, I'm pretty sure that's how I messed this up. Could anyone out there help me out?
Thanks for your time! Bob
My code:
Code:
Sub UserInfo()
Dim olApp As Outlook.Application
Dim objSession As MAPI.Session
Dim objAddrEntries As AddressEntries
Dim objAddrEntry As AddressEntry
Dim objFilter As AddressEntryFilter
Dim strProfileInfo As String
Dim strUserDisplayName As String
Dim strUserBusinessPhone As String
Set olApp = CreateObject("Outlook.Application")
strProfileInfo = "MyServer" & vbLf & "MyMailbox"
strUserDisplayName = Outlook.Application.Session.CurrentUser
Set objSession = olApp.CreateObject("MAPI.session")
objSession.Logon , , False, False, , True, strProfileInfo
Set objAddrEntries = objSession.AddressLists _
("Global Address List").AddressEntries
Set objFilter = objAddrEntries.Filter
objFilter.Fields.Add CdoPR_DISPLAY_NAME, strUserDisplayName
On Error Resume Next
For Each objAddrEntry In objAddrEntries
strUserBusinessPhone = objAddrEntry.Fields _
(CdoPR_OFFICE_TELEPHONE_NUMBER).Value
Next
objSession.Logoff
Set objFilter = Nothing
Set objAddrEntries = Nothing
Set objSession = Nothing
End Sub
support.microsoft.com code:
Code:
Private Sub Command1_Click()
Const strServer = "MyServer"
Const strMailbox = "MyMailbox"
Dim objSession As MAPI.Session
Dim objAddrEntries As AddressEntries
Dim objAddressEntry As AddressEntry
Dim objFilter As AddressEntryFilter
Dim strProfileInfo As String
strProfileInfo = strServer & vbLf & strMailbox
Set objSession = CreateObject("MAPI.Session")
objSession.Logon , , False, False, , True, strProfileInfo
Set objAddrEntries = objSession.AddressLists _
("Global Address List").AddressEntries
Set objFilter = objAddrEntries.Filter
objFilter.Fields.Add CdoPR_SURNAME, "LastName"
objFilter.Fields.Add CdoPR_GIVEN_NAME, "FirstName"
On Error Resume Next
For Each objAddressEntry In objAddrEntries
Debug.Print objAddressEntry.Name
Debug.Print "E-address: " & objAddressEntry.Address
Debug.Print "Display Name: " & objAddressEntry.Fields _
(CdoPR_DISPLAY_NAME).Value
Debug.Print "Title: " & _
objAddressEntry.Fields(CdoPR_TITLE).Value
Debug.Print "Company Name: " & objAddressEntry.Fields _
(CdoPR_COMPANY_NAME).Value
Debug.Print "Office Phone 1: " & objAddressEntry.Fields _
(CdoPR_OFFICE_TELEPHONE_NUMBER).Value
Next
objSession.Logoff
Set objFilter = Nothing
Set objAddrEntries = Nothing
Set objSession = Nothing
End Sub