我在“运行脚本”选择框中看不到我的 VBA 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17493093/
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-08 16:03:15  来源:igfitidea点击:

I cannot see my VBA macro in 'run a script' selection box

vbaoutlook-vba

提问by user2554417

I copied the following code in my oulook VBE, from one of the VBA communities and amended it as per my need. I can run it using F5 and F8. Now I would like to run this macro whenever I receive an email in folder1. I tried setting up a rule but I cannot see the macro listed in the 'run a script' selection box. I have already checked that

我从 VBA 社区之一复制了我的 oulook VBE 中的以下代码,并根据我的需要对其进行了修改。我可以使用 F5 和 F8 运行它。现在,每当我在文件夹 1 中收到电子邮件时,我都想运行此宏。我尝试设置规则,但看不到“运行脚本”选择框中列出的宏。我已经检查过了

  1. macro security setting are correct
  2. macro is in a module not in a class
  1. 宏安全设置正确
  2. 宏在一个模块中而不是在一个类中

can you please tell me what is going wrong in the setting.

你能告诉我设置中出了什么问题吗?

Public Sub SaveAttachments()

    Dim myOlapp As Outlook.Application
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim yourFolder As Outlook.MAPIFolder

    Dim myItem As Outlook.MailItem
    Dim myAttachment As Outlook.Attachment
    Dim I As Long

    Set myOlapp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlapp.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set yourFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

    Set myFolder = myFolder.Folders("folder1")
    Set yourFolder = yourFolder.Folders("folder2")

    For Each myItem In myFolder.Items
        If myItem.Attachments.Count <> 0 Then
            For Each myAttachment In myItem.Attachments
                I = I + 1
                myAttachment.SaveAsFile "C:\arthur\test.csv"

            Next
        End If

        myItem.Move yourFolder

    Next
End Sub

回答by Axel Kemper

To be recognized as proper script macro for the Rule Wizard, the macro has to have the expected parameter:

要被规则向导识别为正确的脚本宏,宏必须具有预期的参数:

Sub myRuleMacro(item as Outlook.MailItem)

MSDN article(still valid for Outlook 2007/2010/2013/2016)

MSDN 文章(对 Outlook 2007/2010/2013/2016 仍然有效)

Related article

相关文章

Article about enabling run-a-script rulesotherwise disabled due to security reasons
(registry key EnableUnsafeClientMailRules).

关于启用脚本运行规则的文章,否则由于安全原因
(注册表项EnableUnsafeClientMailRules)被禁用。

回答by Keith

I had the same issue today on a similar script after Office was upgraded to Version 1803 (Build 9126.2282). Removing the "Pubic" keyword from the sub did the trick. Not sure why, since has been working the other way for years.

在 Office 升级到版本 1803(内部版本 9126.2282)后,我今天在类似的脚本上遇到了同样的问题。从 sub 中删除“Pubic”关键字就成功了。不知道为什么,因为多年来一直在以另一种方式工作。

I also had to re-add the reg key that had disappeared - EnableUnsafeClientMailRules.

我还必须重新添加消失的注册表项 - EnableUnsafeClientMailRules。