2

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
3

3 Answers 3

4

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:

1

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)
0

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
11
  • 2
    Please don't recommend On Error Resume Next to people. I'm sure you understand how to use it properly, but very few people actually understand how it works. Cargo cult programmers copy/paste this stuff and then wonder why their code is acting unexpectedly.
    – RubberDuck
    May 12, 2015 at 14:06
  • @RubberDuck If you know of any other way of dealing with an expected error in VBA, please enlighten me. :) Other than that - I put ample warning in my answer text. I can hardly do anything about disinterested copy/paste programmers.
    – Tomalak
    May 12, 2015 at 14:18
  • Also, when recommending On Error Resume Next be sure that the critical On Error Goto 0 will be executed. In your code, the On Error Goto 0 will be skipped if the Err.Number <> 0 condition is true. Moving On Error Goto 0 to just before the Exit For should do the trick.
    – FreeMan
    May 12, 2015 at 14:18
  • @FreeMan On Error has procedure scope. It will reset automatically when the Sub/Function exits.
    – Tomalak
    May 12, 2015 at 14:19
  • Yeah @Tomalak. I'm aware of an idiomatic way to do it. We shouldn't try to force new paradigms on old languages. This code review answer of mine covers it pretty well.
    – RubberDuck
    May 12, 2015 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.