![]() |
|
|
|
#1
|
|||
|
|||
|
Hi,
I am looking for a solution to a rule I have created in Outlook. I am asking outlook to do a few things. 1. Look for a string of number ex. 5555 2. AND look for a phrase ex. "1 rating" Then 3. Move to this folder ex. "1 Rating Folder" 4. AND forward this email to ex. "example@outlook.com" I am having trouble when an email comes in with other numbers like 5555123 -- outlook sees "5555" inside this string of numbers and applies the rest of the steps. How to I make my rule only look for "5555" by itself, no additional numbers in the front or at the end. Essentially I want to make it an exclusive rule. |
|
#2
|
||||
|
||||
|
You can do it with a script run from a rule. The following assumes the required texts are in the message body. I have included a macro to test it on a file in the inbox. Ensure that you don't have Outlook configured to send immediately while testing.
Code:
Option Explicit
Sub MoveToFolder(olMail As Outlook.MailItem)
Dim strNum As String
Dim strText As String
Dim olOutMail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim strFolder As String
Dim wdDoc As Object
Dim oRng As Object
Dim iCount As Long
iCount = 0
strNum = "<5555>"
strText = "1 rating"
strFolder = "1 Rating Folder" 'Folder must exist!
With olMail
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:=strNum, MatchWildCards:=True)
iCount = iCount + 1
Exit Do
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:=strText, MatchWildCards:=False, MatchCase:=False)
iCount = iCount + 1
Exit Do
Loop
End With
End With
If iCount = 2 Then
Set olOutMail = olMail.Forward
With olOutMail
.To = "example@gmayor.com"
.sEnd
End With
olMail.Move Session.GetDefaultFolder(olFolderInbox).folders(strFolder)
End If
lbl_Exit:
Exit Sub
End Sub
Sub TestItem()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
MoveToFolder olMsg
lbl_Exit:
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
thank you so much for your help. One more question how do multiple rules? Can I put similar code inside the same script?
Example, I want outlook to 1. look for 5555 and 1 rating then if they are both found 1. move to folder 2. forward to jane@example.com Outlook cannot find 5555 then I want it to look for 1111 and 1 rating and repeat additional rules 1. move to folder 2. forward to mike@example.com is there a limit on how many of these rules I can put in place? |
|
#4
|
||||
|
||||
|
You only need the one rule. You can set the searches in the script.
You can loop through a list of numbers and apply the corresponding e-mail addresses. I assume they are all going to the same folder, if not you will need to treat the folders in the same way with a matching folder for each number. The following is not tested, but should work. Code:
Sub MoveToFolder(olMail As Outlook.MailItem)
Dim strText As String
Dim olOutMail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim iCount As Long
Dim vEmail As Variant
Dim vNum As Variant
Dim i As Long
Const strFolder As String = "1 Rating Folder" 'folder must exist
Const strNum As String = "<5555>|<1111>" 'separate search strings with '|'
Const strEmail As String = "jane@example.com|mike@example.com" 'Corresponding e-mail addresses to strNum
strText = "1 rating" 'Common folder
vEmail = Split(strEmail, "|")
vNum = Split(strNum, "|")
Set olInsp = olMail.GetInspector
Set wdDoc = olInsp.WordEditor
For i = LBound(vNum) To UBound(vNum)
With olMail
iCount = 0
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:=vNum(i), MatchWildCards:=True)
iCount = iCount + 1
Exit Do
Loop
End With
Set oRng = wdDoc.Range
With oRng.Find
Do While .Execute(FindText:=strText, MatchWildCards:=False, MatchCase:=False)
iCount = iCount + 1
Exit Do
Loop
End With
End With
If iCount = 2 Then
Set olOutMail = olMail.Forward
With olOutMail
.To = vEmail(i)
.sEnd
End With
olMail.Move Session.GetDefaultFolder(olFolderInbox).folders(strFolder)
End If
Next i
lbl_Exit:
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
| Tags |
| rule |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find number of coma and then add that number of rows in word using macro? | PRA007 | Word VBA | 7 | 05-27-2015 10:45 PM |
Heading's Footnote's number is displayed in TOC. How to remove the number in TOC?
|
Orehrepus | Word | 1 | 07-13-2014 12:51 PM |
How to create a table with a number of line depending a number entered by user
|
Cellendhyll | Word Tables | 3 | 07-10-2014 05:49 AM |
Way to search for a string in text file, pull out everything until another string?
|
omahadivision | Excel Programming | 12 | 11-23-2013 12:10 PM |
Extracting a phone number from a string that contains text and numbers.
|
hommi16 | Excel | 2 | 06-05-2013 09:19 PM |