This run a script macro will open an attached PDF file, look for a keyword and if found, forward the attachment.
If the message you are forwarding contains external content, you may need to respond to a security dialog. In this case, you will need to use a new message and attach the file.
In this example the word you are looking for is the user's alias. If the user's full name as it appears in your contacts or global address list is in the attachment, you can use that as the address and Outlook will resolve it.
Dim strSaveTemp As String Dim sendTo As String ' use this in a rule ' Need to set a reference to Acrobat Type Library Sub CheckAttachment(Item As Outlook.MailItem) Dim oAtt As Attachment strAtt = "" sendTo = "" searchString = "" For Each oAtt In Item.Attachments ' check extension If Right(oAtt.Filename, 4) = ".pdf" Then ' check filename 'If InStr(oAtt.FileName, "keyword") > 0 Then strSaveTemp = "C:\Temp\" & oAtt.Filename oAtt.SaveAsFile (strSaveTemp) searchPDF (strSaveTemp) If sendTo <> "" Then ' forward message Set myForward = Item.Forward ' create new message if forward contains external content ' and triggers a dialog 'Set myForward = Application.CreateItem(olMailItem) With myForward .Recipients.Add sendTo .Subject = oAtt.Filename '.Attachments.Add strSaveTemp .Display 'for testing ' .Send End With Exit Sub ' delete the temp file Kill strSaveTemp End If End If ' delete the temp file Kill strSaveTemp Next oAtt End Sub Sub searchPDF(strSaveTemp As String) Dim searchString As String Dim appObj As Object, AVDocObj As Object Dim arrSearch As Variant 'Check if the file exists. If Dir(strSaveTemp) = "" Then MsgBox "File not found..." Exit Sub End If On Error Resume Next 'Create Adobe Application object. Set appObj = CreateObject("AcroExch.App") 'Check for any errors. If Err.Number <> 0 Then MsgBox "Error in creating the Adobe Application object..." Set appObj = Nothing Exit Sub End If 'Create the AVDoc object. Set AVDocObj = CreateObject("AcroExch.AVDoc") 'Check for any errors. If Err.Number <> 0 Then MsgBox "Error in creating the AVDoc object..." Set AVDocObj = Nothing Set appObj = Nothing Exit Sub End If On Error GoTo 0 'Open the PDF file and check if the open was successful. If AVDocObj.Open(strSaveTemp, "") = True Then Debug.Print strSaveTemp, searchString ' Set up the array these are the words you are searching for arrSearch = Array("poremsky", "slipstick", "cdolive", "jackie", "s.smith", "jacksprat") ' Go through the array and look for a match, then do something For i = LBound(arrSearch) To UBound(arrSearch) Debug.Print arrSearch(i) ' search string, case sensitive, whole words only, start search on page 1 If AVDocObj.FindText(arrSearch(i), False, True, True) = True Then ' Set the To address or name sendTo = arrSearch(i) & "@domain.com" AVDocObj.Close True appObj.Exit 'Release the objects. Set AVDocObj = Nothing Set appObj = Nothing Exit Sub Else: nofound = True End If Next i If nofound = True Then 'If text was not found, close the PDF file and perform clean-up AVDocObj.Close True appObj.Exit 'Release the objects. Set AVDocObj = Nothing Set appObj = Nothing ' comment out or delete this line after testing. MsgBox "The string not found in the PDF file..." End If Else 'PDF file failed to open appObj.Exit 'Release the objects. Set AVDocObj = Nothing Set appObj = Nothing ' comment out or delete this line after testing. MsgBox "Could not open the PDF file..." End If End Sub
If the search word can't used as the To address, you need to use a second array to add the address.
Dim arrTo As Variant ' Set up the array arrSearch = Array("poremsky", "slipstick", "search3", "search4", "search5", "search6", "search7") ' if the search word can't be used as the To address, you need to use a second array. arrTo = Array("firstname.lastname@example.org", "email@example.com", "To3", "To4", "To5", "To6", "To7") ' Go through the array and look for a match, then do something For i = LBound(arrSearch) To UBound(arrSearch) Debug.Print arrSearch(i) ' search string, case sensitive, whole words only, start at beginning If AVDocObj.FindText(arrSearch(i), False, False, True) = True Then sendTo = arrTo(i) AVDocObj.Close True appObj.Exit 'Release the objects. Set AVDocObj = Nothing Set appObj = Nothing Exit Sub Else: nofound = True End If Next i
Test the Rule
You can use this script to test the macro on a message in your mailbox, rather than sending yourself messages to see if the rule works. You can also use this to run the script on messages manually.
To use: paste into the module, select a message and run this macro.
' use this to test the script - select a message and run this macro Sub RunCheckAttachment() Dim objApp As Outlook.Application Dim objItem As Object ' MailItem Set objApp = Application Set objItem = objApp.ActiveExplorer.Selection.Item(1) 'macro name you want to run goes here CheckAttachment objItem End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.
- Go to Tools, References menu.
- Locate the Adobe object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
More information as well as screenshots are at How to use the VBA Editor