Outlook vba match a pattern when attchement is found

hartyshow

New Member
Joined
Mar 17, 2017
Messages
10
I'm trying to match a pattern when a spreadsheet attachment is found in outlook. I can trigger the process for a spreadsheet attachment but don't know how to invoke the search through the attachment.

Code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\form"

     For Each objAtt In itm.Attachments
     
          
     If InStr(objAtt.DisplayName, ".xls") Then

        
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          
          
          Set objAtt = Nothing
        End If
          
          
          
     Next
     End Sub

thanks for your help
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi and welcome to the MrExcel board!
Try this modification of your code:
Rich (BB code):
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
 
  Dim objAtt As Outlook.Attachment
  Dim saveFolder As String, sFileName As String
  saveFolder = "C:\form"
 
  If Not TypeName(itm) = "MailItem" Then Exit Sub
  If Dir(saveFolder, vbDirectory) = "" Then MkDir saveFolder
 
  For Each objAtt In itm.Attachments
    sFileName = LCase(objAtt.Filename)
    If sFileName Like "*.xls" Or sFileName Like "*.xls?" Then
      objAtt.SaveAsFile saveFolder & "\" & objAtt.Filename
    End If
  Next
 
End Sub

Code for the testing:
Rich (BB code):
Sub Test()
  ' Open the mail item with xls attachment(s) and run this code
  saveAttachtoDisk Application.ActiveInspector.CurrentItem
End Sub

In case you are asking for the searching content of attachment then Excel can be involving to open the saved attachment
 
Last edited:
Upvote 0
Thanks ZVI for your reply, I can save a spreadsheet if attachement is .xls but I wanted to search for a Regex like Olus in attachement if .xls file is found then save file in C:\form folder.


If InStr(objAtt.DisplayName, ".xls") if .xls is found Then

search for Regex=Olus and

save file in folder objAtt.SaveAsFile saveFolder & "" & objAtt.DisplayName

the problem now I don't know to how to search through attachement for regex

Any Idea
 
Upvote 0
Do you want to search in file names of the attachments of inside their cells?
 
Upvote 0
To search if "Olus" is present in a file name of the attachment you may use this:
Rich (BB code):
    If sFileName Like "*.xls" Or sFileName Like "*.xls?" Then
      If InStr(1, sFileName, "Olus", vbTextCompare) > 0 Then
        objAtt.SaveAsFile saveFolder & "\" & objAtt.Filename
      End If
    End If
 
Last edited:
Upvote 0
Just for the case you realy need RegEx:
Rich (BB code):
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
 
  Static RegEx As Object
  Dim objAtt As Outlook.Attachment
  Dim saveFolder As String, sFileName As String
  saveFolder = "C:\form"
 
  If Not TypeName(itm) = "MailItem" Then Exit Sub
  If Dir(saveFolder, vbDirectory) = "" Then MkDir saveFolder
 
  If RegEx Is Nothing Then
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Global = True
    RegEx.IgnoreCase = True
    RegEx.Pattern = "Olus"  '<-- use any valid RegExp pattern here
  End If
 
  For Each objAtt In itm.Attachments
    sFileName = LCase(objAtt.Filename)
    If sFileName Like "*.xls" Or sFileName Like "*.xls?" Then
      If RegEx.Test(sFileName) Then
        objAtt.SaveAsFile saveFolder & "\" & objAtt.Filename
      End If
    End If
  Next
 
End Sub
Read about patterns and RegEx usage here - How to use Regular Expressions
 
Upvote 0
we do not have a specific cell, since we receive all spreadsheets from multiple senders. but I'm looking for some specific words each time we receive a spreadsheet, then if the word is found then the spreadsheet need to be archive on the computer.
 
Upvote 0
In what sheet that word(s) is? Code is not a human and requires the strict definitions even if it is "find that word in all sheets"
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,641
Members
452,575
Latest member
Fstick546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top