让 Outlook 2007 运行 VBA 脚本的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/836254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:24:37  来源:igfitidea点击:

Problem Getting Outlook 2007 Running VBA Script

vbaoutlookoutlook-2007outlook-vba

提问by Cyberherbalist

I'm trying to get Outlook to save the attachment in a daily email to a folder where I can have a file system watcher ready to parse and analyze the attachment (it's the report of a data integrity checker). I've set up a Rule that is supposed to run a VBA script, but it just doesn't run as far as I can tell. I've verified in VB6 that the code will in fact save some text to a file, so if Outlook actually runs the VBA script it should be able to do the same. But it doesn't! Can anyone see what the heck I'm doing wrong?

我正在尝试让 Outlook 将日常电子邮件中的附件保存到一个文件夹中,在那里我可以让文件系统观察程序准备好解析和分析附件(这是数据完整性检查程序的报告)。我已经设置了一个应该运行 VBA 脚本的规则,但据我所知它没有运行。我已经在 VB6 中验证了该代码实际上会将一些文本保存到文件中,因此如果 Outlook 实际运行 VBA 脚本,它应该能够执行相同的操作。但事实并非如此!谁能看到我到底做错了什么?

Dim WithEvents objInbox As Outlook.Items

Private Sub Application_Startup()
   Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Sub SnagAttachment(theItem As MailItem)
    On Error Resume Next
    Dim fnum As Integer
    fnum = FreeFile()
    Open "c:\temp\success.txt" For Output As #fnum
    Print #fnum, "Ran SnagAttachment Successfully"
    Close #fnum
End Sub

Note that when I use the Rules wizard, and choose "run a script" the Sub SnagAttachment is listed as a script that can be selected.

请注意,当我使用规则向导并选择“运行脚本”时,Sub SnagAttachment 被列为可以选择的脚本。

回答by JimmyPena

How would you know if it's working or not when you put On Error Resume Nextat the top of the procedure? You would never find out.

当您置于On Error Resume Next程序的顶部时,您如何知道它是否有效?你永远不会发现。

Here are the rules for creating a script that should be run as part of a Rule:

以下是创建应作为规则一部分运行的脚本的规则:

How to create a script for the Rules Wizard in Outlook

如何在 Outlook 中为规则向导创建脚本

Also note the caveat found at How to process incoming messages in Microsoft Outlook:

另请注意如何在 Microsoft Outlook 中处理传入邮件中的警告:

A "run a script" rule is not a good choice for heavy traffic applications, as Outlook is likely to skip applying the rule if too many items arrive that meet the rule's conditions.

“运行脚本”规则对于流量大的应用程序不是一个好的选择,因为如果到达满足规则条件的项目太多,Outlook 可能会跳过应用该规则。

回答by Droter

In order to get the script to work you need to change the security settings in Outlook. Go to Tools > Macro > Security and change it to "Warnings for all macros". Then restart Outlook.

为了使脚本工作,您需要更改 Outlook 中的安全设置。转到“工具”>“宏”>“安全性”并将其更改为“所有宏的警告”。然后重新启动 Outlook。

Hope this helps

希望这可以帮助

回答by bob-the-destroyer

Try isolating the exact problem:

尝试隔离确切的问题:

  1. Check macro security settings. At maximum, it must be set no higher than "Warnings for all macros".
  2. Try creating a new module with a single test sub:
    Sub Test(Item as Outlook.MailItem)
    MsgBox "test"
    End Sub
    
    Then set up a new rule to handle _all_ incoming messages running this sub as the only action, and temporarily disable all other rules. Then send a message to yourself. If you get no popup box as a result, this may be an indication of a bad Outlook install. Try reinstalling it, or calling MS up directly for support.
  3. If the previous test was successful, try working with the `Scripting.FileSystemObject` object instead of `Freefile()` to create and populate files. This is just to test if there's some odd bug you're encountering here. Worth a shot, right?
  4. Make sure your rule conditions are set correctly. There could be a glitch or misspelling in a condition which just drops all messages you want this script to run on.
  1. 检查宏安全设置。最多不得超过“所有宏的警告”。
  2. 尝试使用单个测试子创建一个新模块:
    Sub Test(Item as Outlook.MailItem)
    MsgBox "test"
    End Sub
    
    然后设置一个新规则来处理运行此子程序的 _all_ 传入消息作为唯一操作,并暂时禁用所有其他规则。然后给自己发信息。如果结果没有弹出框,这可能表明 Outlook 安装不正确。尝试重新安装它,或直接致电 MS 寻求支持。
  3. 如果之前的测试成功,请尝试使用 `Scripting.FileSystemObject` 对象而不是 `Freefile()` 来创建和填充文件。这只是为了测试您是否在这里遇到了一些奇怪的错误。值得一试,对吧?
  4. 确保您的规则条件设置正确。在这种情况下可能会出现故障或拼写错误,它只会丢弃您希望此脚本运行的所有消息。

回答by cperlmutter

I was experiencing the same issue, and it seems to me that if there is an error in your code, the script will not even start. This is as opposed to standard VBA where the debugger pops up for runtime errors. For example, I had a label at the end of my function that was missing the colon after it. This caused the script to not run at all (As opposed to running up to this line and then failing). I would suggest commenting out all of your code and starting with just a msgbox "hello world". I would leave this in your code as you debug it to know the code is running, but you will probably have to dismiss the message box many times. Iteratively add back lines of code until you discover where the problem is.

我遇到了同样的问题,在我看来,如果您的代码中有错误,脚本甚至不会启动。这与标准 VBA 不同,在标准 VBA 中,调试器会弹出运行时错误。例如,我的函数末尾有一个标签,后面缺少冒号。这导致脚本根本无法运行(与运行到此行然后失败相反)。我建议注释掉您的所有代码,并从一个 msgbox “hello world”开始。当您调试它以了解代码正在运行时,我会将其保留在您的代码中,但您可能不得不多次关闭消息框。反复添加代码行,直到发现问题所在。