Save outlook attachment without file extensions

jfin1ty

New Member
Joined
May 15, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Hoping this is a simple fix.
I have the below script that I found on another forum that runs as a rule in Outlook to save .pdf email attachments with a timestamp that come from my printer. That part all works fine.
The problem I'm having is that it will save the file with the .pdf as part of the file name.
For example, if the attachment is called test.pdf then my code will run and save it as a .pdf to my documents as "test.pdf (timestamp)".
I think it could be a case of just needing some additional code to get only the attachment's name and not the file extension.
If it helps with a solution, it is always .pdf file types that I run this code for.

VBA Code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
    saveFolder = "C:\Users\Jfin1ty\Documents\"
Dim dateFormat
    dateFormat = Format(Now, "dd-mm-yyyy hh-mm-ss")

     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & objAtt.DisplayName & " " & dateFormat & ".pdf"
          Set objAtt = Nothing
     Next
     
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Can you be more specific as to an example of what one file is being saved as exactly?
 
Upvote 0
Can you be more specific as to an example of what one file is being saved as exactly?
All good Johnny,
Solved it for myself.
In case anyone is running into a similar problem, all I did was change:

VBA Code:
objAtt.SaveAsFile saveFolder & objAtt.DisplayName & " " & dateFormat & ".pdf"

to

VBA Code:
objAtt.SaveAsFile saveFolder & Left(objAtt.DisplayName, Len(objAtt.DisplayName) - 4) & " " & dateFormat & ".pdf"

Which removes the last four characters from the attachment name which in my case was .pdf
 
Upvote 0
Solution
Does the following work also? :

VBA Code:
objAtt.SaveAsFile saveFolder & Left(objAtt.DisplayName, InStrRev(objAtt.DisplayName, ".") - 1) & " " & dateFormat & ".pdf"
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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