Renaming newest File Question

Benzula

Board Regular
Joined
Feb 28, 2014
Messages
248
I have a macro that will save a file directly from Outlook into a directory based on an Outlook Rule. (Currently it is saving in my Documents Folder)

What I want to be able to do, is chain that into another Macro that Renames that file. The main issue I have is that I won't know the name of the file because they like to add random dates to the end of the file. Basically XYZ 4-4-2018 or XYA 04-19-18 which is very inconsistent. Since I have it copy the file over to my directory, I would want another to just rename the most recent file. Or if worse comes to worse, I'll put it into its own directory and just have it be deleted before the rule runs.

So my question is: How do I Rename the Newest File without knowing the exact path name.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Can you store the name (pass as arg) of the file in your macro you currently have and have it rename the file?
 
Upvote 0
This is the macro that I was able to find.

Code:
Public Sub SaveAttachments(Item As Outlook.MailItem)
If Item.Attachments.Count > 0 Then
 
Dim EmAttach As Outlook.Attachments
Dim AttachCount As Long
Dim EmAttFile As String
Dim sFileType As String
Dim i As Long
Set EmAttach = Item.Attachments
    AttachCount = EmAttach.Count
 For i = AttachCount To 1 Step -1
' Get the file name.
 EmAttFile = EmAttach.Item(i).FileName
 
 If LCase(Right(EmAttFile, 5)) = ".xlsx" Then
        ' Get the path to your My Documents folder
        DestFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
        DestFolderPath = DestFolderPath & "\Attachments"
        
        ' Combine with the path to the folder.
        EmAttFile = DestFolderPath & EmAttFile
        
        ' Save the attachment as a file.
        EmAttach.Item(i).SaveAsFile EmAttFile
    End If
 Next i
End If
End Sub
 
Upvote 0
I tried changing the bottom part to be the specific file name I wanted and it didn't work.

I assumed this section would be the file name.

Code:
 ' Save the attachment as a file.
        EmAttach.Item(i).SaveAsFile EmAttFile

I tried changing it to
EmAttach.Item(i).SaveAsFile 'Test'
 
Last edited:
Upvote 0
I also tried to add another Dim at the start and use that.

Dim EmAttRename As String

Set EmAttRename = "Test" or 'Test' or Test
EmAttach.Item(i).SaveAsFile EmAttRename
 
Last edited:
Upvote 0
I also tried to add another Dim at the start and use that.

Dim EmAttRename As String

Set EmAttRename = "Test" or 'Test' or Test
EmAttach.Item(i).SaveAsFile EmAttRename


Hmmm maybe change it here:

Code:
' Combine with the path to the folder.
[COLOR=#ff0000]EmAttFile = "Testnemwfilename"[/COLOR]
        EmAttFile = DestFolderPath & EmAttFile
 
Upvote 0
It says "Cannot Save the attachment. Path does not exist. Verify the path is Correct"

Code:
Public Sub SaveAttachments(Item As Outlook.MailItem)
If Item.Attachments.Count > 0 Then
 
Dim EmAttach As Outlook.Attachments
Dim AttachCount As Long
Dim EmAttFile As String
Dim sFileType As String
Dim EmFileName As String
Dim i As Long
 
Set EmAttach = Item.Attachments
    AttachCount = EmAttach.Count
 For i = AttachCount To 1 Step -1
' Get the file name.
 EmAttFile = EmAttach.Item(i).FileName
 
 If LCase(Right(EmAttFile, 5)) = ".xlsx" Then
        ' Get the path to your My Documents folder
        DestFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
        DestFolderPath = DestFolderPath & "\Attachments"
        
        ' Combine with the path to the folder.
        EmAttFile = "TestNameFileName"
        EmAttFile = DestFolderPath & EmAttFile
        
        ' Save the attachment as a file.
        EmAttach.Item(i).SaveAsFile EmFileName
    End If
 Next i
End If
End Sub
 
Upvote 0
Actually it worked! I forgot I changed the variable to EmFileName. However it didn't save it as an XLS so Ima try and add that in the name.
 
Upvote 0
W00T, It worked when I added .xlsx. Here is the final code that is working.

Code:
Public Sub SaveAttachments(Item As Outlook.MailItem)
If Item.Attachments.Count > 0 Then
 
Dim EmAttach As Outlook.Attachments
Dim AttachCount As Long
Dim EmAttFile As String
Dim sFileType As String
Dim i As Long
 
Set EmAttach = Item.Attachments
    AttachCount = EmAttach.Count
 For i = AttachCount To 1 Step -1
' Get the file name.
 EmAttFile = EmAttach.Item(i).FileName
 
 If LCase(Right(EmAttFile, 5)) = ".xlsx" Then
        ' Get the path to your My Documents folder
        DestFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
        DestFolderPath = DestFolderPath & "\Attachments"
        
        ' Combine with the path to the folder.
        EmAttFile = "TestNameFileName.xlsx"
        EmAttFile = DestFolderPath & EmAttFile
        
        ' Save the attachment as a file.
        EmAttach.Item(i).SaveAsFile EmAttFile
    End If
 Next i
End If
End Sub

Thank you so much again.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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