leemcder
08-11-2020, 06:43 AM
I've started using this code which saves .doc attachments which hit my outlook mailbox. Is it possible to only save the attachment if the file names contains the word "client" ?
any help with this is much appreciated.
Many thanks
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim strSubject As String
Dim strName As String
strSubject = "Our Ref: " & Right(itm.Subject, Len(itm.Subject) - InStrRev(itm.Subject, Chr(32)))
saveFolder = "Y:\accounts\success fee bills\"
For Each objAtt In itm.Attachments
If Right(LCase(objAtt.FileName), 4) = ".doc" Then
strName = CleanFileName(strSubject & ".doc", ".doc")
objAtt.SaveAsFile saveFolder & strName
End If
Next objAtt
Set objAtt = Nothing
End Sub
Public Function CleanFileName(strFilename As String, strExtension As String) As String
' Graham Mayor
' A function to ensure there are no illegal filename
' characters in a string to be used as a filename
' strFilename is the filename to check
' strExtension is the extension of the file
Dim arrInvalid() As String
Dim vfName As Variant
Dim lng_Name As Long
Dim lng_Ext As Long
Dim lngIndex As Long
' Ensure there is no period included with the extension
strExtension = Replace(strExtension, Chr(46), "")
' Record the length of the extension
lng_Ext = Len(strExtension)
' Remove the path from the filename if present
If InStr(1, strFilename, Chr(92)) > 0 Then
vfName = Split(strFilename, Chr(92))
CleanFileName = vfName(UBound(vfName))
Else
CleanFileName = strFilename
End If
' Remove the extension from the filename if present
If Right(CleanFileName, lng_Ext + 1) = "." & strExtension Then
CleanFileName = Left(CleanFileName, InStrRev(CleanFileName, Chr(46)) - 1)
End If
' Define illegal characters (by ASCII CharNum)
arrInvalid = Split("9|10|11|13|34|42|47|58|60|62|63|92|124", "|")
' Add the extension to the filename
CleanFileName = CleanFileName & Chr(46) & strExtension
' Remove any illegal filename characters
For lngIndex = 0 To UBound(arrInvalid)
CleanFileName = Replace(CleanFileName, Chr(arrInvalid(lngIndex)), Chr(95))
Next lngIndex
lbl_Exit:
Exit Function
End Function
any help with this is much appreciated.
Many thanks
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim strSubject As String
Dim strName As String
strSubject = "Our Ref: " & Right(itm.Subject, Len(itm.Subject) - InStrRev(itm.Subject, Chr(32)))
saveFolder = "Y:\accounts\success fee bills\"
For Each objAtt In itm.Attachments
If Right(LCase(objAtt.FileName), 4) = ".doc" Then
strName = CleanFileName(strSubject & ".doc", ".doc")
objAtt.SaveAsFile saveFolder & strName
End If
Next objAtt
Set objAtt = Nothing
End Sub
Public Function CleanFileName(strFilename As String, strExtension As String) As String
' Graham Mayor
' A function to ensure there are no illegal filename
' characters in a string to be used as a filename
' strFilename is the filename to check
' strExtension is the extension of the file
Dim arrInvalid() As String
Dim vfName As Variant
Dim lng_Name As Long
Dim lng_Ext As Long
Dim lngIndex As Long
' Ensure there is no period included with the extension
strExtension = Replace(strExtension, Chr(46), "")
' Record the length of the extension
lng_Ext = Len(strExtension)
' Remove the path from the filename if present
If InStr(1, strFilename, Chr(92)) > 0 Then
vfName = Split(strFilename, Chr(92))
CleanFileName = vfName(UBound(vfName))
Else
CleanFileName = strFilename
End If
' Remove the extension from the filename if present
If Right(CleanFileName, lng_Ext + 1) = "." & strExtension Then
CleanFileName = Left(CleanFileName, InStrRev(CleanFileName, Chr(46)) - 1)
End If
' Define illegal characters (by ASCII CharNum)
arrInvalid = Split("9|10|11|13|34|42|47|58|60|62|63|92|124", "|")
' Add the extension to the filename
CleanFileName = CleanFileName & Chr(46) & strExtension
' Remove any illegal filename characters
For lngIndex = 0 To UBound(arrInvalid)
CleanFileName = Replace(CleanFileName, Chr(arrInvalid(lngIndex)), Chr(95))
Next lngIndex
lbl_Exit:
Exit Function
End Function