Forward Outlook Email by Filtering using Macro Rule

Status
Not open for further replies.
use the tester macro & change the name of the script you want to run - then use Step into in the VB Editor to watch either step.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
saveAttachtoDiskRule objItem

End Sub
 
This seems to be working ok here in my limited tests - i have it popping up a message box if the macro runs but doesn't have the < in the body. I also limited the number to 2 digits - it should work with any number of digits but won't find single digits.

Code:
Public Sub FWItem(Item As Outlook.MailItem)
Dim Email As Outlook.MailItem
Dim Matches As Variant
Dim RegExp As Object
Dim Pattern As String

Set RegExp = CreateObject("VbScript.RegExp")

If TypeOf Item Is Outlook.MailItem Then

Pattern = "(\s[<]\s?[0-9]{2})"
With RegExp
.Global = False
.Pattern = Pattern
.IgnoreCase = True
Set Matches = .Execute(Item.Body)
End With

If Matches.Count > 0 Then
Debug.Print Item.Subject ' Print on Immediate Window
Set Email = Item.Forward
Email.Subject = Item.Subject
Email.Recipients.Add "alias@domain.com"
Email.Save
Email.Display
Else
MsgBox "no match"

End If
End If

Set RegExp = Nothing
Set Matches = Nothing
Set Email = Nothing
Set Item = Nothing
End Sub
Hi Diane,

I tried it, but it didn't do anything. Please help.

Regards,
 
use the tester macro & change the name of the script you want to run - then use Step into in the VB Editor to watch either step.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
saveAttachtoDiskRule objItem

End Sub

Sorry to be obtuse, but I don't know what needs to be typed here:

'macro name you want to run goes here

Am I creating a new macro, copy/pasting your script and then what? I appreciate all of your help btw Diane!!
 
The Sub RunScript() macro is used to trigger the script named in the line under "'macro name you want to run goes here" on the selected message - it allows you to test the script without sending email or running rules manually.

The name would be the name of the run a script macro you want to test, which is used in your rule. Two macros are entered in this thread -FWItem and saveAttachtoDiskRule - so for this thread, it would be whichever of those scripts you are using.

To use it, select a message you want to test the script on and click in the RunScript macro then click Run.
 
I tried it, but it didn't do anything. Please help.
In the line before Set RegExp = CreateObject("VbScript.RegExp"), add
msgbox "macro running"

then send a test message (or use the RunScript macro to run the macro on a message that has the < in it). Does the message box pop up?
 
Sorry to be obtuse, but I don't know what needs to be typed here:

'macro name you want to run goes here

Am I creating a new macro, copy/pasting your script and then what? I appreciate all of your help btw Diane!!

When I step through, it goes from Sub RunScript(), but skips down to Set objApp = Application, then the next 2 lines...nothing else happens.
 
When I step through, it goes from Sub RunScript(), but skips down to Set objApp = Application, then the next 2 lines...nothing else happens.
That's how it's supposed to work... but it should hit the next line and work down the code. Does the macro stop and highlight a line in yellow?

After this line saveFolder = "\\Servername\foldername\test\"
add msgbox savefolder

this will pop up a dialog showing the folder name.
Also, try putting the path to your my documents folder there - that will tell us if it's failing on the path.
 
Oh, and make sure error handling is off - if on error resume next is green, it's off, but if its no, add a ' as the first character to comment it out. We want to see where the errors come up.
 
That's how it's supposed to work... but it should hit the next line and work down the code. Does the macro stop and highlight a line in yellow?

After this line saveFolder = "\\Servername\foldername\test\"
add msgbox savefolder

this will pop up a dialog showing the folder name.
Also, try putting the path to your my documents folder there - that will tell us if it's failing on the path.

Seems so, no?
upload_2017-6-7_8-58-5.png
 
Oh, and make sure error handling is off - if on error resume next is green, it's off, but if its no, add a ' as the first character to comment it out. We want to see where the errors come up.

Everything is yellow only when I step through it...
 
Everything is yellow only when I step through it...
oh sorry, i forgot the current line will be yellow when you step through - if it's running normally, it's yellow where it stops due to error. :(

On the path line - After this line saveFolder = "\\Servername\foldername\test\" - does it have an ending \? if not, the file is saved in the previous folder with a name like 'my documentsfilename.ext"
 
oh sorry, i forgot the current line will be yellow when you step through - if it's running normally, it's yellow where it stops due to error. :(

On the path line - After this line saveFolder = "\\Servername\foldername\test\" - does it have an ending \? if not, the file is saved in the previous folder with a name like 'my documentsfilename.ext"

If I drill down to the folder location it says: C:\Users\de63\Documents\ yet nothing is going there...nor if I do it \\DE63Win7T460s\c$\Users\de63\Documents\...very odd. What should I be looking for in the folder btw, a simple test file?
 
What should I be looking for in the folder btw, a simple test file?
You're using saveAttachtoDiskRule ? That would save any attachments in the message to the designated folder. If the message doesn't have an attachment, you won't have anything saved.
 
You're using saveAttachtoDiskRule ? That would save any attachments in the message to the designated folder. If the message doesn't have an attachment, you won't have anything saved.

That was the issue! I was using that when actually the rule in Outlook was pointing to AttachtoDisk! So now I'll test pointing to a network server and keep you posted, I can't thank you enough Diane!!
 
You're using saveAttachtoDiskRule ? That would save any attachments in the message to the designated folder. If the message doesn't have an attachment, you won't have anything saved.

Diane, weird thing now...when I run it in VB I get an error:

upload_2017-6-7_13-23-4.png


And when I hit Debug it highlights this line now:
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

I used to be able to hit Save, then recompile but now it's greyed out...

Yet if I go back into Outlook and run the rule one time, it moves the file.
 
In the line before Set RegExp = CreateObject("VbScript.RegExp"), add
msgbox "macro running"

then send a test message (or use the RunScript macro to run the macro on a message that has the < in it). Does the message box pop up?
Hi Diane,

I tried to add the Msgbox, and when a new message arrives, it shows up, but seems, it did not scan the email body as even there's a < symbol on it, it didn't forward the email.

Thank you,
 
Hi Diane,

Is it possible that instead of searching the < symbol on the email body, we will pop up the message using message box and the script will search it in the message box?

Regards,
 
And when I hit Debug it highlights this line now:
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

I used to be able to hit Save, then recompile but now it's greyed out...

Yet if I go back into Outlook and run the rule one time, it moves the file.

I'll need to look up that error... but do you have the new conversation mode enabled or did you select a group header or a non-mail item? The first causes all sorts of errors.
 
Hi Diane,

Below is my new code. I just edited the pattern, and it is almost working. My concern right now is if the < has no space on the character, it did not forward to my email.

Public Sub FWItem(Item As Outlook.MailItem)
Dim Email As Outlook.MailItem
Dim Matches As Variant
Dim RegExp As Object
Dim Pattern As String

Set RegExp = CreateObject("VbScript.RegExp")
MsgBox "macro running"

If TypeOf Item Is Outlook.MailItem Then

Item.BodyFormat = olFormatPlain
strBody = Item.Body

Pattern = "(\s[<]\s|\s[<]\s?[0-9]{1}|\s[<]\s?[0-9]{2})"
With RegExp
.Global = False
.Pattern = Pattern
.IgnoreCase = True
Set Matches = .Execute(Item.Body)
End With

If Matches.Count > 0 Then
Debug.Print Item.Subject ' Print on Immediate Window
Set Email = Item.Forward
Email.Subject = Item.Subject
Email.Recipients.Add "alias@domain.com"
Email.Save
Email.Send
Else
MsgBox "no match"

End If
End If

Set RegExp = Nothing
Set Matches = Nothing
Set Email = Nothing
Set Item = Nothing
End Sub


Here is a sample email:

That was fast! It only took <seconds to reboot the modem.

Thanks,
 
Pattern = "(\s[<]\s|\s[<]\s?[0-9]{1}|\s[<]\s?[0-9]{2})"
These are the same pattern, so is redundant - \s[<]\s|\s[<]\s? - ? means you'll have 0 or 1 of the character before it (so, basically, optional). \s<\s? should be all you need - it should work with or without the [] - the [] denote a character class but its a single character so isn't needed and i don't think < is used in VB Regex as a special character (it is used in some flavors of regex). ([ ] are used with [0-9] to mean any of these digits)

This is also redundant - [0-9]{1}|\s[<]\s?[0-9]{2})"- [0-9] means any 1 number (for single digit, you can use [1-9] if you don't expect 0 seconds to reboot). [0-9]{1,2} is 1 to 2 of these digits (* is any number of digits).

\s<\s?[0-9]{1,2} or \s[<]\s?[0-9]{1,2} is the same as what you are using (and easier to read).

Here is a sample email:

That was fast! It only took <seconds to reboot the modem.
is this phrase always going to be the same? if so, this should work -
Pattern = "It only took ([<]\s?([0-9]*))"
or
Pattern = "(<\s?([0-9]*) to"
or the really lazy way:
Pattern = "It only took(.*)to"

This is a pretty good reference for patterns - Reference - What does this regex mean? - Stack Overflow
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
B Forward every other email in Outlook 2013 Outlook VBA and Custom Forms 2
A Creating an outlook rule to forward an email with a specific message Using Outlook 1
A Outlook 2010 - Want to Have All Email Reply Forward as Rich Test Using Outlook 7
M In Outlook Calendar remove the buttons: 'Today' and '<' (Back a day) and '>' (Forward a day) that are below the Ribbon and above the calendar display. Using Outlook 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
N Going Forward: Using Outlook 2016 for RSS Feeds Using Outlook 2
N Outlook script to forward emails based on senders' address Outlook VBA and Custom Forms 2
J Outlook 2010: can't forward appointment on custom form Using Outlook 1
T outlook 2013 does not mark multiple forward messages as forward Using Outlook 2
S Outlook 2k/2k3 Reply or Forward: Signature image replaced by original sender Using Outlook 3
S Outlook 2003 Prefix on reply and forward Using Outlook 1
K How to auto truncate a message to forward with Outlook Rules Using Outlook 6
V major issue in outlook "Change in forward mail also change selected mail " Outlook VBA and Custom Forms 1
A why outlook change forward mail Outlook VBA and Custom Forms 2
H Forward E-mails at Certain Times in Outlook using VBA Outlook VBA and Custom Forms 1
T forward event in outlook with c# Outlook VBA and Custom Forms 1
A Forward mails with "FW:" or "RE:" in title Using Outlook 2
J Outlook 365 Forward Email Subject to my inbox when new email arrive in shared inbox Using Outlook 0
D Outlook 365 Forward Meeting Related Messages to Specific Meeting Organizer Outlook VBA and Custom Forms 0
T Customized form: The Forward option shows write layout Outlook VBA and Custom Forms 0
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
F Forward incoming email with 4 embedded images in the body without original sender Outlook VBA and Custom Forms 22
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
S Auto forward for multiple emails Outlook VBA and Custom Forms 0
G Forward email body to other mail list directly from Exchange server Exchange Server Administration 1
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
Bering Forward selected email without the original email appended Outlook VBA and Custom Forms 0
C UDFs Reply vs Forward Outlook VBA and Custom Forms 3
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
C How to rename subject line and forward the email Outlook VBA and Custom Forms 2
R Error when trying to forward current email item Outlook VBA and Custom Forms 7
G Missing forward/replied icons Using Outlook 2
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
M Forward Appointment as BCC with VBScript Outlook VBA and Custom Forms 7
D Disable or hide "reply" and "reply to all" and "forward" in email from access vba Outlook VBA and Custom Forms 1
Sabastian Samuel HOW DO I FORWARD AN EMAIL WITH MACRO using an email that in the body of another email Outlook VBA and Custom Forms 3
C Don't forward duplicate Using Outlook 0
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
I change subject and forward without FW: Outlook VBA and Custom Forms 4
C VBA to Forward e-mails from certain address and between certain times Outlook VBA and Custom Forms 1
J Forward Action in Form Outlook VBA and Custom Forms 1
J Auto Forward - Include Attachment and change Subject depending on original sender Outlook VBA and Custom Forms 3
K add pdf to every reply or forward Outlook VBA and Custom Forms 1
Brostin Forward a mail to the address listed in the email text Outlook VBA and Custom Forms 1

Similar threads

Back
Top