Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
You'll look around on the Internet and discover that error occurs because, really, only ONE instance of Outlook is meant to be open at a time. And so you'll remember to manually close Outlook before you run your code -- until you get sick of that. And you'll poke around a little harder at the internet and you'll find
Set objOutlook = GetObject(, "Outlook.Application")
This gets the open instance of Outlook for your use.
Excellent.
Right up until you didn't have Outlook open again. And then you're back to 429 again
'Double crud!'
So, you'll growl and curse and do something like
On Error Resume NextDim objOutlook As Outlook.ApplicationSet objOutlook = GetObject(, "Outlook.Application")If Err.Number = 429 Then Err.Clear Set objOutlook = CreateObject("Outlook.Application")End If
And you'll pat yourself on the back for a job well done...
Until the day you come to recall exactly WHY On Error Resume Next
is such a bad idea.
That day when some other error besides our friend 429 happens and cascades down your code causing you grey and lost hair as you try to find the real cause instead of the point where your code couldn't ignore its own errors anymore. And on that day, when you decide to get rid of that pesky On Error Resume Next statement, you'll realize that you are stuck.
Because, with Outlook, that 429 error is a way of life.
So what to do? Minimize the damage that On Error Resume Next can do!
VBA is perfectly capable of creating functions that return objects like Outlook.Application objects
Like this function:
Option ExplicitPublic WasOpen As BooleanPrivate Function FireOutlook() As Outlook.ApplicationOn Error Resume NextDim objOutlook As Outlook.ApplicationSet objOutlook = GetObject(, "Outlook.Application")'MsgBox Err.Number & " " & Err.DescriptionIf Err.Number = 429 Then Err.Clear WasOpen = False ' Create the Outlook session. Set objOutlook = CreateObject("Outlook.Application")Else WasOpen = TrueEnd IfSet FireOutlook = objOutlookEnd Function
If Outlook was open, then you have an Outlook UI (mail, calendars, contacts, etc) in existence already.
Great!
But if it wasn't, then you'll need to get the Outlook UI up and going.
For mail items, that's where WasOpen comes into play
Dim ns As Outlook.NamespaceDim Folder As Outlook.MAPIFolderSet ns = objOutlook.GetNamespace("MAPI")Set Folder = ns.GetDefaultFolder(olFolderInbox)Set objOutlookExplorers = objOutlook.ExplorersIf WasOpen = False Then objOutlook.Explorers.Add Folder Folder.Display 'done openingEnd If
So to recap, automating the opening of Outlook for use in VBA code is going to lead to unavoidable 429 errors, but On Error Resume Next plays havoc with complex code. The solution is to isolate the opening of Outlook into its own function to contain the error, and return the Outlook object to the procedure that needed it. Whether you calling VBA code comes from Word, Excel, Access or some other source -- even VBScript -- the code demonstrated is THE correct way to open Outlook.
And while I suppose that there are some folks who would prefer a code sample with Late Binding, if you are sophisticated enough to know what that is and want it, then you are sophisticated enough to change the Dim statements to Objects and suss out the numeric equivalent of the Outlook constants in use. Me -- I like Intellisense!
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
I did not follow everything for I am still learning. Can I get a copy of all code needed to open Outlook? I do want to studt it but need to make sure I have it working. Thank you john@my-planet-earth.com
Alternatively, the following code handles Error 429 in particular, and stops on other errors.
On Error GoTo Err_Handler
...
...
Err_Handler:
If Err.Number = 429 Then
Err.Clear
Set oOutlookApp = GetObject(, "Outlook.Application")
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End If
Have a question about something in this article?
You can receive help directly from the article author.
Sign up for a free trial to get started.
Comments (3)
Commented:
Commented:
Commented:
On Error GoTo Err_Handler
...
...
Err_Handler:
If Err.Number = 429 Then
Err.Clear
Set oOutlookApp = GetObject(, "Outlook.Application")
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End If