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

跟風遠走 提交于 2019-12-17 18:20:00

问题


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

  1. macro security setting are correct
  2. macro is in a module not in a class

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

回答1:


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)

Related article

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




回答2:


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.

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



来源:https://stackoverflow.com/questions/17493093/i-cannot-see-my-vba-macro-in-run-a-script-selection-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!