Script to use in a rule when a message arrives to send to email in the message

Status
Not open for further replies.

snoopyb98

Member
Outlook version
Outlook 2010 32 bit
Email Account
POP3
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 from other places on the forum, 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!
 
Re: Script to use in a rule when a message arrives to send to email in the mes

Does the code sorta/mostly work? Especially the first part? (i don't have messages to test it and don't have time to create some right now).

Add a

msgbox strAlias

after the 'end if' to see if its picking up the address.

This should be just the string containing the address -

objMsg.Recipients.Add strAlias

If you don't want to use a template, use

set objmsg = Application.CreateItem(olMailItem)
 
Re: Script to use in a rule when a message arrives to send to email in the mes

Well, let's say it this way, when I run the rule with the script, I don't get any errors, but I also don't show that I've sent an email (or have one display if coded).

I've attempted to clean up the code, but that doesn't seem to do anything more (or worse):
PHP:
Sub Message1(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 olMail = Application.ActiveExplorer().Selection(1)   ' Debug.Print olMail.Body     Set Reg1 = New RegExp    Set Reg2 = New RegExp        strID = Item.EntryID    Set oItem = Application.Session.GetItemFromID(strID)         ' \s* = invisible spaces    ' \d* = match digits    ' \w* = match alphanumeric         With Reg1        .Pattern = "(Email Address:\s*[:]\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 
 
MsgBox strAlias        Dim objMsg As MailItemSet objMsg = Application.CreateItemFromTemplate("C:\Documents and Settings\Elizabeth  Shewmaker\My Documents\Frist Email.oft") objMsg.Recipients.Add strAlias & "[a-z0-9.]*)@([a-z0-9.]*)"objMsg.Display 
 
End Sub

Would it be possible also that I have the script in the wrong thing?

screenshot.jpg
 
Re: Script to use in a rule when a message arrives to send to email in the mes

This works for me - it's a run a rule script and works in either a module or thisoutlooksession. I added the msgboxes so you can see the result of each regex.

Because its a run a script rule and the mailitem is declared in the sub, we don't need the message id or to switch it to oitem.

Code:
Sub First(Item As Outlook.MailItem)
   Dim Reg1 As RegExp
   Dim Reg2 As RegExp
   Dim M1 As MatchCollection
   Dim M As Match
   Set Reg1 = New RegExp
   Set Reg2 = New RegExp
    
  With Reg1
       .Pattern = "(Email Address:\s*(\d*)\s*)"
       .Global = True
   End With
 
   If Reg1.test(Item.Body) Then
       Set M1 = Reg1.Execute(Item.Body)
       For Each M In M1
         strcode = M.SubMatches(1)
         MsgBox "reg1: " & strcode
       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
         MsgBox "reg2: " & strAlias
       Next
   End If 
 
Dim objMsg As MailItem 
 
Set objMsg = Application.CreateItem(olMailItem) 
 
objMsg.Recipients.Add strAlias 
 
objMsg.Subject = "New Subject Title" & strcode 
 
objMsg.Display '.Send 
 
Set objMsg = Nothing 
 
End Sub
 
Re: Script to use in a rule when a message arrives to send to email in the mes

It pulls the e-mail address, however a new e-mail is never created.
 
Re: Script to use in a rule when a message arrives to send to email in the mes

My sample too? I replaced the call to a template with a blank message so we wouldn't have to worry about template problems until after we knew it worked.

It could mean the code is failing, but if you see the msgboxes, it gets all the data you need creating the message is simple. So simple, Outlook can't screw it up. :)
 
Diane -
I just joined so I could thank you for your helpful information. I've done a lot of VBA programming with excel, but never with outlook, and the info from these posts from last year helped me do exactly what I needed. Thank you!
 
Hi Diane

Just joined the forum to say thank you for an excellent and informative post that helped me tweak as well as shorten my code, so big thanks once again.

I will greatly appreciate if you could advise how can I replace text in template body e.g. Find "Customer" in "Dear Customer" and replace with the string stored in "strcode" and leaving rest of the text as it is.

I tried adding objMsg.Body = Dear & strcode in above code, it does work but rest of the text in template body disappears.

Thank you & best regards
 
This : objMsg.Body = Dear & strcode replaces the message body. Try objMsg.Body = Dear & strcode & vbcrlf & objmsg.body

If there is only one instance of Customer used, you can replace it using Replace() - Replace(objmsg.body. "Customer", strcode) - or use Replace(objmsg.body. "Dear Customer", "Dear " strcode).
 
Hi again Diane

Thank you for your great help the other day.
I'm now stuck again, I'm trying to fetch email address from incoming message formatted as below:

Email: abcdefg.2015@abcdemail.com

I've tried .Pattern = "([a-z0-9.]*)@([a-z0-9.]*)" which works absolutely fine but there're other email addresses before and after Email: as well that I don't need, I only need email address that follows Email: then I tried .Pattern = "(Email[:]([a-z0-9.]*)@([a-z0-9.]*))" but the code fails to run. As there are some spaces after Email: I think there might be a line break as well that causing code to fail, I will heartily appreciate if you could help me please, thank you & best regards
 
Hi Diane

I'll greatly appreciate if you could help me with this, thank you very much.
 
I tried .Pattern = "(Email[:]([a-z0-9.]*)@([a-z0-9.]*))" but the code fails to run. As there are some spaces after Email: I think there might be a line break as well that causing code to fail, I will heartily appreciate if you could help me please, thank you & best regards

if there are spaces between email: and the address, try \s*. if there is a space following the address, use either "(Email[:]([a-z0-9.]*)@([a-z0-9.]*) )" or "(Email[:]([a-z0-9.]*)@([a-z0-9.]*)\s)"

(Sorry, I missed this earlier - I'm assuming you got it working by now, I added my comments for the benefit of others.)
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Diane Poremsky Run a Script Rule: Send a New Message when a Message Arrives Using Outlook 2
L Moving Message Class email via script and Rule Outlook VBA and Custom Forms 3
G Script in rule to send to multiple emails found in message bo Outlook VBA and Custom Forms 11
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
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
Vijay Error in rule- Run a script Using Outlook 1
L Run a Script Rule doesn't work Using Outlook 5
S using script rule to save attachments on arrival Outlook 2010 Outlook VBA and Custom Forms 9
L Cannot run script from rule Outlook VBA and Custom Forms 7
O modify vba to run it as script rule Outlook VBA and Custom Forms 8
A Creating archive rule on the clients by script/ Outlook VBA and Custom Forms 3
Jeff Rott Diane Question on "Use in a Run a Script Rule" Outlook VBA and Custom Forms 1
M Outlook Rule To Redirect Mail - Script to overcome lack of cc's & others Using Outlook 0
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
D Troubleshooting rule/script not running Outlook VBA and Custom Forms 5
H Rule Wizard and script to initiate e-mail Outlook VBA and Custom Forms 2
L Run script rule not exectued for first few mails Outlook VBA and Custom Forms 2
R Script for simplifying spam control Outlook VBA and Custom Forms 7
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
W Designer Form 2013 and Script ? how ? Outlook VBA and Custom Forms 1
G print attachment straight away; working script edit not working Outlook VBA and Custom Forms 0
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
G Script does not exist Outlook VBA and Custom Forms 0
G Trigger script without restaring outlook Outlook VBA and Custom Forms 7
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
L VB script only runs manually Outlook VBA and Custom Forms 5
D.Moore VB script to Digitaly Sign newly created outlook message Outlook VBA and Custom Forms 2
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
D.Moore VBA script fail after Office 365 update Using Outlook 8
M Outlook 2013 Script Assistance - Save Opened Link with Subject Added Outlook VBA and Custom Forms 1
F Script for zip file attachment Outlook VBA and Custom Forms 1
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
Y Outlook 2013 Run A Script Outlook VBA and Custom Forms 4
Z Script to set account? Using Outlook 0
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
J Calling a Public sub-routine from the script editor via VB script Outlook VBA and Custom Forms 4
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
H VB script in outlook form doesn't work anymore Outlook VBA and Custom Forms 2
A Script to fetch data from mails in restricted collection and sending them to excel Using Outlook 1
B Wanting to run a script that will filter any body that has a russian link in it. Outlook VBA and Custom Forms 5
Bri the Tech Guy Registry Tweak to make "Run a Script" Action Available Outlook VBA and Custom Forms 2
V VB script code to save a specific email attachment from a given email Outlook VBA and Custom Forms 14

Similar threads

Back
Top