In Outlook, is it possible to run a macro on an email I select manually?

谁都会走 提交于 2021-01-27 13:58:52

问题


Is it possible to run a macro on an email that I manually select in my inbox. For instance, right click on the email and select "Send to >> Macro >> (show list of subroutines accepting Outlook.MailItem as a parameter)?


回答1:


I think you will have to add a Button to the mail-ribbon. This Button can call an Routine.

In this Routine you use the active selection:

Sub example()
Dim myOlApp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Dim olExplorer As Explorer
Dim olfolder As MAPIFolder
Dim olSelection As Selection
Dim olitem As mailitem

Set olExplorer = Application.ActiveExplorer
Set olfolder = Application.ActiveExplorer.CurrentFolder

If olfolder.DefaultItemType = olMailItem Then
Set olSelection = olExplorer.Selection
end if 

For Each olitem In olSelection
'do something
Next olitem

end sub

I hope you get this working... Max




回答2:


So, I was able to simplify Max's answer a bit, but he certainly pointed me in the right direction. Below is basically what I'm going with. After selecting an email in my inbox, I should be able to run this macro and proceed to work on it.

Sub example()
    Dim x, mailItem As Outlook.mailItem
    For Each x In Application.ActiveExplorer.Selection
        If TypeName(x) = "MailItem" Then
            Set mailItem = x
            call fooMail(mailItem)
        End If
    Next
End Sub

Sub fooMail(ByRef mItem as Outlook.MailItem)
    Debug.print mItem.Subject
End Sub


来源:https://stackoverflow.com/questions/25315291/in-outlook-is-it-possible-to-run-a-macro-on-an-email-i-select-manually

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