rule to change subject, pull email addresses from body, and forward with templ

Status
Not open for further replies.

moe0303

Member
template.

Hello,

I want to create a rule which will be applied to emails with specifc text in the subject field. I want to "reply" to the email, but I want the reply to go to an email address contained within the message body (not the original sender). I also want to change the subject to "Heat Ticket" and a number pulled from the message body. Once all that is sorted, the rule would send a template to the new email address.

I see the option in the rules to run a script. Can I use that to complete this action? If so, how? I'm not a programer and have very limited code experience.

-Moe

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

Yes, you would use Run a Script. I have sample scripts available.

Is the address (and number) you want to use easily identifiable in the message? Ie, you can search for a specific word or phrase and the address always follows.

Links to my samples are at Outlook's Rules and Alerts: Run a Script - Slipstick Systems

You'll also need to get the address and number - Use RegEx to extract text from an Outlook email message - Slipstick Systems

moe0303

Member
Re: rule to change subject, pull email addresses from body, and forward with t

I'm not sure I understand how to implement (or modify) your code for my purposes. The number will always follow the word "Ticket". I would like to pull any email address within the message body. The only thing that I can guarantee will be the same each time is the domain of the email. Using the information from article about extracting text, I came up with the following:

> pattern="(Ticket\s*(\d*)\s*)"

' Starts with "Ticket" + some white space + some digits + white space

> pattern="((\w*)@hotmail.com\s*)"

'starts with some alphanumeric string +"@hotmail.com" + some white space.

Are those correct?

Once I have extracted the text, how do I get the information to do what I want?

==========================================

Sample message:

Ticket 123456789 has been assigned.

==========================================

I would want the rule to send a pre-prepared message with the subject "Work Ticket 12345678" to moe0303@hotmail.com.

I know I would have to create a template and I think I can do that, but I don't know how to write the code.

Can this be done?

Thanks again for all your help.

-Moe

moe0303

Member
Re: rule to change subject, pull email addresses from body, and forward with t

Dear moderators,

I apologize for trying to reply to this thread 1000 times. I didn't see the pop-up till just now which says it will have to be approved by a moderator, and I feel like a fool. Please forgive me.

-Moe

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

Not a problem - I'm not sure what flagged it for moderation...

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

This code should get you halfway there (assuming I didn't make any stupid mistakes in it, i didn't test it) - i need to check on how to get the address since it's a second pattern rather than part of the number pattern.

Code:


Sub ChangeSubjectForward(Item As Outlook.MailItem)

Dim oItem As Outlook.MailItem
Dim Reg1 As RegExp
Dim Reg2 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim EntryID As New Collection
Dim strID As String

Set Reg1 = New RegExp
Set Reg2 = New RegExp

strID = Item.EntryID
Set oItem = Application.Session.GetItemFromID(strID)

With Reg1
.Pattern = "(Ticket\s*(\d*)\s*)"
.Global = True
End With

If Reg1.test(oItem.Body) Then
Set M1 = Reg1.Execute(oItem.Body)
For Each M In M1
strCode = M.SubMatches(1)
Next
End If

With Reg2
.Pattern = "((\w*)@hotmail.com\s*)"
.Global = True
End With

If Reg2.test(Item.Body) Then
Set M1 = Reg2.Execute(Item.Body)
For Each M In M1
strAlias = M.SubMatches(1)
Next
End If

'--------------

'Item.Subject = "Heat Ticket " & strCode
'Item.Save

Set myForward = Item.Forward

myForward.Subject = "Heat Ticket " & strCode

myForward.Display 'Send

Set oItem = Nothing

End Sub

- - - Updated - - -

BTW, for testing, i have it set to display the message rather than send it.

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

I updated the code above - it worked in a simple test here. I changed it so the subject is changed only in the forward and FW is removed. If you want the FW, use the original line which i commented out.

moe0303

Member
Re: rule to change subject, pull email addresses from body, and forward with t

I'm getting a "user defined type not defined" error on the following line: Dim Reg1 As RegExp

Is RegExp something I have to install?

Senior Member

moe0303

Member
Re: rule to change subject, pull email addresses from body, and forward with t

Awesome! Sorry I missed that part.

I actually like having it display the message for now.

Now, how do I get it to pre populate a message? I have a template named "Review" if that helps.

-Moe

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

you'll need a slightly different code for below the '---- line in the macro - from Run a Script Rule: Send a new message when a message arrives - Slipstick Systems

Set myForward = Item.Forward
myForward.Subject = "Heat Ticket " & strCode

myForward.Display 'Send

Set oItem = Nothing

use

Dim objMsg As MailItem

Set objMsg = Application.CreateItemFromTemplate("C:\path\to\review.oft")

objMsg.Subject = "Heat Ticket " & strCode

objMsg.display '.Send

Set objMsg= Nothing

moe0303

Member
Re: rule to change subject, pull email addresses from body, and forward with t

Thank you so much.

It's working great except for one little quirk. If the email address uses a ".", it wont catch the whole first part. For example, some people use their first.mi.last@hotmail.com. and it only catches last@hotmail.com.

I tried switching the \w* to \W*, but that didn't work either.

Diane Poremsky

Senior Member
Re: rule to change subject, pull email addresses from body, and forward with t

Try
.Pattern = "(([a-z0-9.]*)@hotmail.com\s*)"

That works as i just tested it. (.*) works too, but it gets the entire line - Outlook resolves what it sees as the address but but the display name looks awful

snoopyb98

Member
Re: rule to change subject, pull email addresses from body, and forward with t

Hi,

What I'm wanting the script to do is this: extract the email address from an email, which is listed after "Email Address:" in the email, create a new message in which the recipient is that extracted email address with a new subject and the body of the email to be from an oft, then sent.

So, I need a little help. I have formulated a script with the information here, part of it worked one time, I made an adjustment, then the script doesn't seem to work at all, or do anything, but there are no errors that it brings up. This is what I have:

PHP:
Sub First(Item As Outlook.MailItem)
Dim oItem As Outlook.MailItem
Dim Reg1 As RegExp
Dim Reg2 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim EntryID As New Collection
Dim strID As String
Set Reg1 = New RegExp
Set Reg2 = New RegExp

strID = Item.EntryID
Set oItem = Application.Session.GetItemFromID(strID)

With Reg1
.Global = True
End With

If Reg1.test(oItem.Body) Then
Set M1 = Reg1.Execute(oItem.Body)
For Each M In M1
strCode = M.SubMatches(1)
Next
End If

With Reg2

.Pattern = "([a-z0-9.]*)@([a-z0-9.]*)"
.Global = True
End With
If Reg2.test(Item.Body) Then
Set M1 = Reg2.Execute(Item.Body)
For Each M In M1
strAlias = M.SubMatches(1)
Next
End If

'------------
'Item.Subject = "New Subject Title" & strCode
'Item.Save

Set oItem = Nothing

Dim objMsg As MailItem

Set objMsg = Application.CreateItemFromTemplate("C:\Documents and Settings\Name\My Documents\First Email.oft")

objMsg.Subject = "New Subject Title" & strCode

objMsg.Display '.Send

Set objMsg = Nothing

End Sub

Any and all help would be greatly appreciated. I've dabbled in writing scripts before, but I'm by no means proficient in them. They are great when they work!

Thanks!

Status
Not open for further replies.
Thread starter Title Forum Replies Date
vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
rule to Find some words in the subject, change subject then forward Using Outlook 1
Change order of actions in one (!) rule Outlook VBA and Custom Forms 2
Change identity or "from" field if a rule matches Outlook VBA and Custom Forms 14
Outlook 365 Rule runs manually but returns the error code "an unexpected error has occurred" when incoming mail arrives Using Outlook 0
Finding a rouge rule... Using Outlook 6
How to get this rule to work Using Outlook 0
Seeking help with Outlook rule Using Outlook 2
Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
Save attachment run a script rule Outlook VBA and Custom Forms 0
Rogue Outlook Rule ? Using Outlook 2
Can I create a Rule that sends me an email when I get a Task? Using Outlook 2
Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
How create a Rule to filter sender's email with more that one @ sign Using Outlook 1
Incorrectly Setup a Rule at Domain level to not allow address from outside domain Exchange Server Administration 2
Transport Rule to detect Keyword question.. Exchange Server Administration 2
"Words In Recipient's Address" Rule Causes Outlook To Stop Processing Rules Using Outlook 3
Outlook Autoforward rule do not work for NDR messages Using Outlook 1
Setup autofoward rule on a particular folder in Outlook Using Outlook 0
Rule for "on behalf of" - with changing names Using Outlook 2
Unable to remove rule outlook 2010 Using Outlook 0
Outlook 2016 This rule will only run when you check your email in Outlook.... Using Outlook 4
Rule to move attachments with specific name Outlook VBA and Custom Forms 1
Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
Rule to move (specific) messages from Sent folder to Specific folder Using Outlook 1
Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
Rule based on sender / wrong sender sent to folder Using Outlook 7
Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
Making rule to move email to folder from one O365 domain Using Outlook 1
Assess content of User Defined Field in Rule Using Outlook 3
Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
HELP- Rule to auto strip prepend from external emails Using Outlook 0
Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
rule to flag messages not working Using Outlook 5
Exchange Online Everything a Transport Rule should do and cannot Exchange Server Administration 1
Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
saving attachement to folder named the same as rule name Outlook VBA and Custom Forms 0
Can I create a Rule with Or'd conditions? Using Outlook 1
Rules - how to determine if all conditions in rule are true or false Outlook VBA and Custom Forms 5
Error in rule- Run a script Using Outlook 1
Rule behavior analysis - any tools available? Using Outlook 0
Creating a rule in outlook to filter messages Using Outlook 0
Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
VBA Rule for removing all body but hyperlink then forwarding Outlook VBA and Custom Forms 9
custome rule Outlook VBA and Custom Forms 2
Mark Sent Items as Read After Copying with a Rule Using Outlook 0
Run a Script Rule: Send a New Message when a Message Arrives Using Outlook 2