PDA

View Full Version : Rename attachments, depending on original name



killer
06-02-2017, 08:46 AM
I have some working code (thanks to code I found online and adapted and code from Graham Mayor on here) which saves attachments to a folder based on the subject line:-

so if the subject lines reads "BillBates" all the attachments are saved in a folder called "BillBates". This is all working fine. (The system is not allowing me to post this existing code)

I now need to enhance this:- if an attachment's name starts with the characters "F1" I want it's name to be changed, so it starts with "Face". For example, if the original filename is "F1-RH161FD.jpg" I want the attachment renamed to be ""Face-RH161FD.jpg". There will be several of these types of name change. (R1 = Rear" etc).

My gut feeling is that it would be better to FIRST run the VBA to rename the files, then run the one that saves the attachments to a folder.

I've found many examples of saving attachments with different names but not the scenario I require. Can anyone advise please?

TIA

Steve

gmayor
06-02-2017, 08:32 PM
You can do it at the same time by examining the filename and making the correction as required. At some point you will have got the filename of the attachment and assigned it to a variable e.g.


strfname = olAttach.fileName
immediately after that line add the following:

Select Case Left(strfname, 3)
Case "F1-": strfname = "Face-" & Mid(strfname, 4)
Case "R1-": strfname = "Rear-" & Mid(strfname, 4)
Case Else
End Select
Provide an appropriate case statement for each change. Case Else will ensure that those filenames that don't fit the criteria are not changed.

killer
06-04-2017, 11:33 AM
Hi Graham

thanks again. I'll be looking at this tomorrow, so should have an update (and hopefully a result) then.

Cheers!

Steve

killer
06-05-2017, 07:59 AM
Hi

I've got this working perfectly, thank you!

I have one more quick question, happy to post it separately if that's more appropriate, BUT . . . in my previous version it created the saved attachment's path and filename as a hyperlink. I lifted the code below, placed it below the line that reads "Next j" and thought it would work fine, but it's just saving as text, no link (I've checked and the original email is html format).

Steve


' Adds the filename string to the message body and saves it
' Check for HTML body
If olMsg.BodyFormat <> olFormatHTML Then
olMsg.Body = olMsg.Body & vbCrLf & "The file(s) were saved to " & strSaveFldr & olMsg.Subject
Else
olMsg.HTMLBody = olMsg.HTMLBody & "<p>" & "The file(s) were saved to " & strSaveFldr & olMsg.Subject
End If