user969113
user969113

Reputation: 2429

VBA Try and Catch (MS Outlook)

I use the following function to monitor a public Outlook folder for new E-Mails to arrive:

Public Sub Application_Startup()

   Set NewMail = Application.GetNamespace("MAPI").Folders(3).Folders(2)....

End Sub

For some reason, the path to this public folder changes over time in a weekly period. What changes is the number in .Folders(3). It varies from 1 to 3. Apparently, to keep the function working and to catch the error, when the path changes, I want to implement a try and catch function such as:

Try {
    Set NewMail = Application.GetNamespace("MAPI").Folders(1).Folders(2)....
Catch Error

Try {
    Set NewMail = Application.GetNamespace("MAPI").Folders(2).Folders(2)....
Catch Error

Try {
    Set NewMail = Application.GetNamespace("MAPI").Folders(2).Folders(2)....
Catch Error

Since I am new to VBA I am struggeling implementing this Try and Catch function. Can anyone assist with VBA code in Outlook?

I think one solution I just manage to implement is:

For i = 1 To 3

    On Error Resume Next

    Set NewMail = Application.GetNamespace("MAPI").Folders(i).Folders(2)....

Next i

Upvotes: 2

Views: 9665

Answers (3)

Tomalak
Tomalak

Reputation: 338228

VB(A) does not have structured error handling, therefore it does not have try and catch statements.

If you expect a statement to fail, On Error Resume Next with a following If Err.Number <> 0 Then ... check is the way to go. It's best to keep the sections of code that are covered by On Error Resume Next to an absolute minimum. You don't want to silently swallow unexpected run-time errors, as this causes nasty and hard-to-find bugs.

Of course writing code in a fail-safe manner is another possibility.

Indexing into the Folders collection by number is not fail-safe, as you have noticed. Indexing numerically into an unknown collection of items should generally be avoided. Lucky for you the Folders collection allows indexing by name. .Folders("Foo") is a lot better than .Folders(3).

Of course there might be no subfolder named "Foo", but that can be handled via On Error.

Here is an alternative approach that uses a path (backslash-separated, not including the \\Public Folders - mailbox name\All Public Folders\ part):

Function GetPublicFolderByPath(path As String) As Folder
  Dim parent As Folder, part As Variant

  Set parent = GetNamespace("MAPI").GetDefaultFolder(olPublicFoldersAllPublicFolders)

  For Each part In Split(path, "\")
    On Error Resume Next
    Set GetPublicFolderByPath = parent.Folders(part)

    If Err.Number <> 0 Then
      Set GetPublicFolderByPath = Nothing
      Exit For
    End If

    On Error GoTo 0
  Next part
End Function

Use like this

Dim f As Folder
Set f = GetPublicFolderByPath("Folder A\Folder B\Folder C")

If f Is Nothing Then
    ' not found
Else
    ' use f
End If

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65431

You can avoid the Error by checking the Count property of the Folders Collection first, for example:

Set NewMail = Application.GetNamespace("MAPI").Folders(Application.GetNamespace("MAPI").Folder‌​s.Count).Folders(2)

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

Error handling in VBA is based on the On Error statement. The following articles explains how to handle errors in VBA:

Try/Catch blocks is for add-ins.

Note, you can use the NameSpace.GetDefaultFolder method to get the All Public Folders folder in the Exchange Public Folders store. You just need to pass the olPublicFoldersAllPublicFolders value. See the sample code on the following pages:

Upvotes: 4

Related Questions