VBA 尝试和捕捉(MS Outlook) [英] VBA Try and Catch (MS Outlook)

查看:103
本文介绍了VBA 尝试和捕捉(MS Outlook)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下功能来监控公共 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

出于某种原因,此公用文件夹的路径每周都会随时间变化.改变的是.Folders(3) 中的数字.它从 1 到 3 不等.显然,为了保持函数正常工作并捕获错误,当路径发生变化时,我想实现一个 try 和 catch 函数,例如:

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

由于我是 VBA 新手,我正在努力实现这个 Try and Catch 功能.任何人都可以协助 Outlook 中的 VBA 代码?

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

推荐答案

VBA 中的错误处理基于 On Error 语句.以下文章解释了如何在 VBA 中处理错误:

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

Try/Catch 块用于加载项.

Try/Catch blocks is for add-ins.

注意,您可以使用 NameSpace.GetDefaultFolder 方法来获取 Exchange 公用文件夹存储中的所有公用文件夹文件夹.您只需要传递 olPublicFoldersAllPublicFolders 值.请参阅以下页面上的示例代码:

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:

这篇关于VBA 尝试和捕捉(MS Outlook)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆