idWinter
idWinter

Reputation: 340

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

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)?

Upvotes: 3

Views: 3947

Answers (2)

idWinter
idWinter

Reputation: 340

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

Upvotes: 3

Max
Max

Reputation: 759

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

Upvotes: 3

Related Questions