PDA

View Full Version : Change file name and keep extension



emccracken
06-13-2018, 08:48 AM
I'm working with Outlook and used a combination of a Rule and VBA to automatically save files of a specific name to a folder. When I try to enter my own file name, it corrupts the file. It won't let me edit the name. I'm using this:


For Each objAtt In itm.Attachments
If InStr(objAtt.DisplayName, ".xlsx") Then
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " - " & objAtt.DisplayName
End If
Set objAtt = Nothing
Next



This adds the date before the current file name and saves it to a specified location. I'm looking to edit my code to save files, keep the extension (to avoid corrupting the file), and change the name of the file. What should I use to call the current extension of the file?

One other issue: I currently have it set to only save .xlsx files because the signature contains a picture that automatically saves. Is there a way to not download the .jpg or .png file, rather than only download the .xlsx?

gmayor
06-13-2018, 07:53 PM
The following should work


If Not objAtt.fileName Like "image*.*" Then
strFname = objAtt.fileName
strExt = Right(strFname, Len(strFname) - InStrRev(strFname, Chr(46)) + 1)
strFname = "My New Filename" & strExt
objAtt.SaveAsFile strSaveFldr & strFname
End If