Rename Excel file from Outlook

wallen1605

Board Regular
Joined
Dec 15, 2017
Messages
57
Hi Guys

I know technically this is a question for Outlook, however the issue is with the Excel file I am trying to open.

Basically, I have a rule in outlook that checks incoming emails for an attachment and if found, it renames and saves the attachment to a network folder with todays date and time. All the attachments are successfully renaming and saving, however, when I open each of the files, I receive an error stating the file is either corrupt or the file extension is not valid. I have tested the incoming files before renaming and they open fine, it is only when the code renames them that I cannot open them?

My code example is below, please could someone help me out as I have exhausted the web for the correct answer and cannot find it.

Code:
Sub SaveAttachmentsToDisk(itm As Outlook.MailItem)Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim att As Object
saveFolder = [URL="file://\\server\share"]\\server\share[/URL]  ' change to your path
For Each objAtt In itm.Attachments
        If itm.Attachments.Count > 0 Then
        
            For Each att In itm.Attachments
            
                If att.FileName Like "*.xlsx" Then
                objAtt.SaveAsFile saveFolder & "" & Format(Now, "dd-mm-yy-hh-mm-ss") & ".xlsx"
    
                itm.UnRead = False
    
                End If
    
            Next att
    
        End If
Next
End Sub

EDIT: Backslash is in my code but not showing in the following line when I post to the forum between the "".
Code:
objAtt.SaveAsFile saveFolder & "" & Format(Now, "dd-mm-yy-hh-mm-ss") & ".xlsx"
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Darn, your code works for me. I even tried it both ways, with "\\server\path" and "H:\path," and it worked both ways. I'd suggest trying with other types of files to see if the problem is Outlook or Excel.

While it's not the cause of the problem, I'll point out a logic error in your code. There are two loops through itm.Attachments, one with objAtt and the other with att. Also, the if statement between the two loops will always be true. You just need the inner loop, and I think (didn't try) you can get away without the if test because if the item has no attachments, the loop will fall through.

Good luck. Anything to do with VBA can be maddening!
 
Upvote 0
Many thanks for your answer, it appears the double loop was the issue, because it was actually saving the 'temp' file (file to be renamed) and then saving it before the actual file is saved, so it was the two loops causing the issue. I know this because the save file was 1kb in size when the actual file should have been 13kb in the test I did.

Modified the code with only 1 loop, and the issue is now resolved.

Thanks for your help, another lesson for me in VBA, always double check my loops :)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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