Outlook saves excel file automatically with VBA but corrupts file

Tosborn

New Member
Joined
May 24, 2016
Messages
44
Hi all,

I'm using the following piece of VBA in outlook to save a file when it is received automatically however the file is unusable.

If I don't change the name of the file when saving in the directory it is ok but because the file names can be different at times the VBA changes the file name and this results in corruption. The file name changes but not the extension.

Any ideas?
Code:
'Outlook code


'save all attachments




Public Sub SaveAllAttach(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "M:\Sales Team\Budgets\FY 2016_17"
'saveFolder = "H:"


     For Each objAtt In itm.Attachments
     
            'to save as original file name - this works ok
            'objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
            
            'to save as specified filename - currently using this, which corrupts the file
            objAtt.SaveAsFile saveFolder & "\" & "Forecast Funding Report Summary.xlsx"
            
          Set objAtt = Nothing
     Next
End Sub

Cheers,
Tim
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
what value does objAtt.DisplayName return? To test it exactly use debug.print "["&objAtt.DisplayName&"]" and see what is returned to the immediate window
 
Upvote 0

Forum statistics

Threads
1,214,379
Messages
6,119,190
Members
448,874
Latest member
Lancelots

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