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

Status
Not open for further replies.

moe0303

Member
Outlook version
Email Account
Exchange Server
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.

Thanks in advance for any help you can give.

-Moe
 
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
 
Re: rule to change subject, pull email addresses from body, and forward with t

Thanks so much for your reply, Diane.

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.

please contact moe0303@hotmail.com with any questions.

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

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
 
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
 
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.Recipients.Add strAlias & "@hotmail.com"
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.
 
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.
 
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?
 
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.

Thanks again for your help.

-Moe
 
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

instead if

Set myForward = Item.Forward
myForward.Recipients.Add strAlias & "@hotmail.com"
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.Recipients.Add strAlias & "@hotmail.com"

objMsg.Subject = "Heat Ticket " & strCode

objMsg.display '.Send

Set objMsg= Nothing
 
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.


Any advice?
 
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




results.png
 
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
       .Pattern = "(Email Address:\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 = "([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.Recipients.Add strAlias & "[a-z0-9.]*)@([a-z0-9.]*)" 
 
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.
Similar threads
Thread starter Title Forum Replies Date
diver864 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
V rule to Find some words in the subject, change subject then forward Using Outlook 1
A Change order of actions in one (!) rule Outlook VBA and Custom Forms 2
B Change identity or "from" field if a rule matches Outlook VBA and Custom Forms 14
E Create Rule to Forward Email if Subject Begins With Using Outlook 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
CWM330 This rule has a condition that the server cannot process? Using Outlook 1
A rule name into message - how? Outlook VBA and Custom Forms 5
O Mail rule issue Using Outlook 3
A manual rule sends mail to wrong folder Using Outlook 5
Aussie Outlook 365 Rule runs manually but returns the error code "an unexpected error has occurred" when incoming mail arrives Using Outlook 1
bhamberg Finding a rouge rule... Using Outlook 6
D How to get this rule to work Using Outlook 0
B Seeking help with Outlook rule Using Outlook 2
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
G Save attachment run a script rule Outlook VBA and Custom Forms 0
R Rogue Outlook Rule ? Using Outlook 2
P Can I create a Rule that sends me an email when I get a Task? Using Outlook 2
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
M How create a Rule to filter sender's email with more that one @ sign Using Outlook 1
1 Incorrectly Setup a Rule at Domain level to not allow address from outside domain Exchange Server Administration 2
J Transport Rule to detect Keyword question.. Exchange Server Administration 2
T "Words In Recipient's Address" Rule Causes Outlook To Stop Processing Rules Using Outlook 3
R Outlook Autoforward rule do not work for NDR messages Using Outlook 1
R Setup autofoward rule on a particular folder in Outlook Using Outlook 0
N Rule for "on behalf of" - with changing names Using Outlook 2
S Unable to remove rule outlook 2010 Using Outlook 0
O Outlook 2016 This rule will only run when you check your email in Outlook.... Using Outlook 4
Nadine Rule to move attachments with specific name Outlook VBA and Custom Forms 1
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
O Rule to move (specific) messages from Sent folder to Specific folder Using Outlook 1
S Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
K VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
icacream Rule based on sender / wrong sender sent to folder Using Outlook 7
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
L Making rule to move email to folder from one O365 domain Using Outlook 1
J Assess content of User Defined Field in Rule Using Outlook 3
S Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
J HELP- Rule to auto strip prepend from external emails Using Outlook 0
A Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
A rule to flag messages not working Using Outlook 5
Brian Murphy Exchange Online Everything a Transport Rule should do and cannot Exchange Server Administration 1
J Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
A saving attachement to folder named the same as rule name Outlook VBA and Custom Forms 0
Jennifer Murphy Can I create a Rule with Or'd conditions? Using Outlook 1

Similar threads

Back
Top