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

Status
Not open for further replies.

moe0303

Member
Outlook version
Outlook 2007
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
Outlook version
Outlook 2007
Email Account
Exchange Server
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
 

moe0303

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
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
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
Outlook version
Outlook 2007
Email Account
Exchange Server
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?
 

moe0303

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

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

moe0303

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

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

snoopyb98

Member
Outlook version
Outlook 2010 32 bit
Email Account
POP3
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.
Top