Home > Mobile >  VBA outlook rule which filters body source
VBA outlook rule which filters body source

Time:10-19

I'm looking to create a simple rule but outlook doesn't allow me to do it, VBA maybe? I saw that we could use the MailItem.HTMLBody element but I did not understand how to do it ...

It is simply a matter of detecting the term "email <o: p>" in the source of the received email. Once transmitted, I want to move the email to a folder already created, or to be created by the script.

Thanks for your help

Sub filter(Item As Outlook.MailItem)
    Dim source As String
    Dim ns As Outlook.NameSpace
    Dim MailDest As Outlook.Folder
    Set ns = Application.GetNamespace("MAPI")
    source = Item.HTMLBody
    If InStr(source, "email<o:p>") > 0 Then
        Set MailDest = ns.Folders("Personal Folders").Folders("test")
        Item.Move MailDest
End If

If I remove "Item", do I have to use ThisOutlookSession object ?

And then, what are the precise steps to get it triggered? When I create a rule that detects the object and I do the following I don't have "run macro" in the list ... Do you know how to do it please?

I need to create a rule "If email from [email protected]" that triggers this vba but I can't find that in outlook rules manager...

EDIT solution:

  • Activate Run a script in regedit
  • Activate Developer menu and Macro
  • Use the script below
  • Create the rule
Public Sub filter(ByRef MItem As Outlook.MailItem)
    Dim source As String
    Dim ns As Outlook.NameSpace
    Dim MailDest As Outlook.Folder
    Set ns = Application.GetNamespace("MAPI")
    Set myInbox = ns.GetDefaultFolder(olFolderInbox)
    source = MItem.HTMLBody
    If InStr(source, "email<o:p>") > 0 Then
        MItem.Move myInbox.Folders("MyFolder")
    End If
End Sub

CodePudding user response:

This may not technically be an answer but putting it here anyway in case it's of use to someone.
I don't know if it's close enough, but I've quickly adapted a code I use that filters through an entire folder for emails matching certain conditions (in this case I've reset the conditions to 'email from [email protected] and contains email<o:p>' and moves them to a separate folder. It might be just what you need, it might not. What it will do is trigger from a button (pro tip, can be assigned a button in your home tab or custom tab of your choice) on the active folder in outlook.
Give it a try;

Sub filter()
'Variables:
Dim Item As Outlook.MailItem, MyFolder As Outlook.Folder, MailDest As Outlook.Folder
'Set fixed Variables:
With Application
    Set MyFolder = .ActiveExplorer.CurrentFolder 'This means the sub will work on whatever the active folder is at the minute.
    Set MailDest = .GetNamespace("MAPI").Folders("Personal Folders").Folders("test") 'I like doing things all in one go if poss.
End With

With MyFolder
    For a = .Items.Count To 1 Step -1
        If TypeOf .Items(a) Is MailItem Then
            If InStr(.Items(a).HTMLBody, "email<o:p>") > 0 _
            And .Items(a).SenderEmailAddress Like "[email protected]" Then
                .Items(a).Move MailDest
            End If
        End If
    Next
End With 'MyFolder

End Sub

CodePudding user response:

This worked fine

  • Activate Run a script in regedit
  • Activate Developer menu and Macro
  • Use the script below
  • Create the rule
Public Sub filter(ByRef MItem As Outlook.MailItem)
    Dim source As String
    Dim ns As Outlook.NameSpace
    Dim MailDest As Outlook.Folder
    Set ns = Application.GetNamespace("MAPI")
    Set myInbox = ns.GetDefaultFolder(olFolderInbox)
    source = MItem.HTMLBody
    If InStr(source, "email<o:p>") > 0 Then
        MItem.Move myInbox.Folders("MyFolder")
    End If
End Sub
  • Related