Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Outlook VBA code to be used in other programs

Status
Not open for further replies.

BobEulberg

Programmer
Feb 22, 2005
15
0
0
US
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:
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
 
Hi BobEulberg,

Have you tried simply
Code:
[blue]Set objSession = CreateObject("MAPI.session")[/blue]

CreateObject is a VBA function, not an application function.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks Tony, As much as I like an easy answer, I'd feel less like a moron if it was at least a little more complicated. :(

So I copied the microsoft.com code directly into my project to avoid my changes completely. But now I get the same error --Run-time error '13': Type mismatch-- on this line:

Code:
Set objAddrEntries = objSession.AddressLists _
                           ("Global Address List").AddressEntries

Any thoughts on that? Thanks again! Bob
 
Hi Bob,

I'm way out of my depth on this one. I know virtually nothing about CDO [smile]

Do you have an addresslist called "Global Address List" - or is that some generic name that should always exist? I tried running the code - just using a number instead of the name and it worked for me. Sorry not to be more helpful.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top