Hello,
I have an excel macro to get contact info from outlook 2007
I am getting runtime errors or crashes.
I have created a little test macro to show the problems
You need to add a reference to "Microsoft CDO 1.21 Library" and "Microsoft Outlook 12.0 Object Library"
test1 fails
test2 crashes
test3 produces a runtime error
test4 does work because of two facts:
1) error is skipped
2) i = mapiAddressEntries.Count is read
without 2) test4 will fail
thanks
I have an excel macro to get contact info from outlook 2007
I am getting runtime errors or crashes.
I have created a little test macro to show the problems
You need to add a reference to "Microsoft CDO 1.21 Library" and "Microsoft Outlook 12.0 Object Library"
Code:
Option Explicit
Dim mapiSession As MAPI.Session
Dim mapiAddressList As MAPI.addressList
Dim mapiAddressEntries As MAPI.AddressEntries
Dim mapiAddressEntry As MAPI.AddressEntry
Dim mapiEntryFilter As MAPI.AddressEntryFilter
Dim accountName As String
Dim displayName As String
Public Sub testMAPI()
' use a real contact account name here
accountName = "accName"
Set mapiSession = CreateObject("mapi.session")
Call mapiSession.Logon(, , False, False, , True)
Set mapiAddressList = mapiSession.GetAddressList(CdoAddressListGAL)
Set mapiAddressEntries = mapiAddressList.AddressEntries
Set mapiEntryFilter = mapiAddressEntries.Filter
mapiEntryFilter.Fields.Add CdoPR_ACCOUNT, accountName
'Call test1
'Call test2 ' follow instructions inside this function: breakpoint is required
'Call test3
'Call test4
' if test2 is running and instructions have been done followed correctly
' then EXCEL will crash here when executing the line below
' but if you add a breakpoint here it will not crash
mapiSession.logoff
Set mapiAddressEntry = Nothing
Set mapiEntryFilter = Nothing
Set mapiAddressEntries = Nothing
Set mapiAddressList = Nothing
Set mapiSession = Nothing
End Sub
Private Sub test1()
For Each mapiAddressEntry In mapiAddressEntries
' displayName will not be set even if there are some entries
displayName = mapiAddressEntry.Name
Next mapiAddressEntry
End Sub
Private Sub test2()
' 1) add a breakpoint here
' 2) when code reaches this point then expend mapiTest and then mapiAddressEntries
' 3) continue
For Each mapiAddressEntry In mapiAddressEntries
displayName = mapiAddressEntry.Name
Next mapiAddressEntry
End Sub
Private Sub test3()
Dim i As Long
' Runtime error -2147220992 (80040200)
' Collaboration Data Objects MAPI_E_END_OF_SESSION
i = mapiAddressEntries.Count
For Each mapiAddressEntry In mapiAddressEntries
displayName = mapiAddressEntry.Name
Next mapiAddressEntry
End Sub
Private Sub test4()
Dim i As Long
On Error Resume Next
i = mapiAddressEntries.Count
For Each mapiAddressEntry In mapiAddressEntries
displayName = mapiAddressEntry.Name
Next mapiAddressEntry
End Sub
test2 crashes
test3 produces a runtime error
test4 does work because of two facts:
1) error is skipped
2) i = mapiAddressEntries.Count is read
without 2) test4 will fail
thanks