rename Files once received on outlook & displaced in a separate File

EL Mehdi

New Member
Joined
Nov 23, 2016
Messages
4
Hello,
This is my first day in the forum so I want to say Hello to everybody :),
I m new in VBA so please support me if I made any mistakes :LOL:
If possible, I m asking help regarding the issue bellow,
I found a small code to copy/past attached files from received emails to a specific file,
The problem is some attached files have a space " " in the end of their names so I want to copy/past them & Correct the name by removing the space,
Now after excexuting my code, the file are attached normally but once a file with " " is detected, no more file are attached,

Unfortunately I can't attache any file as the code is in outlook

Here after my code :

Code:
Public Sub s2aveAttachtoRapportDetailléParEquipeFile(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Users\LZYRFY\Desktop\Nouveau dossier (2)"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          FileName = CreateObject("Scripting.FileSystemObject").GetBaseName(objAtt)
          File1 = "C:\Users\LZYRFY\Desktop\Nouveau dossier (2)\" & FileName
          MsgBox (File1)
                If Right(FileName, 1) = " " Then
                    FileName = Left(FileName, Len(FileName) - 1)
                    File2 = "C:\Users\LZYRFY\Desktop\Nouveau dossier (2)\" & FileName
                    MsgBox (File2)
                    Name File1 As File2
                End If
          Set objAtt = Nothing
     Next
End Sub

Thank you for your support
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

EL Mehdi

New Member
Joined
Nov 23, 2016
Messages
4
It s ok I found a solution :cool:
Code:
Public Sub s2aveAttachtoRapportDetailléParEquipeFile(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Users\LZYRFY\Desktop\Nouveau dossier (2)\"
     For Each objAtt In itm.Attachments
        TheFileName = objAtt.FileName
        If Left(Right(TheFileName, 5), 1) = " " Then
               TheFileName = Left(TheFileName, Len(TheFileName) - 5)
               TheFileName = TheFileName & ".xls"
        End If
            objAtt.SaveAsFile saveFolder & TheFileName
          Set objAtt = Nothing
     Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,272
Messages
5,985,684
Members
439,974
Latest member
sjoerdbosch

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
Top