2

I am currently receiving weekly reports on outlook which I need to open and save in a specific folder. I have succeeded in renaming the file and transferring it to the desired file.

HOWEVER, the file format isn't the same as the file which is attached to the email, it is either registered as type "file" when I do not put a date format at the end or a type ".2016" file when I put one. When opened in Notepad the information is unreadable

Here is the code I currently use:

Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormatdateFormat = Format(Now, "dd.mm.yyyy")
saveFolder = "C:\Users\mypathtotheattachment"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "thenewnameofmyattachment" & ".csv"
Next
End Sub

Any help is welcome, I scanned all over the place for any information but I'm stuck...

Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Antoine Myrd
  • 35
  • 1
  • 4
  • What happens if you leave the name the same? E.g. if you do objAtt.SaveAsFile objAtt FileName? – Charlie May 23 '16 at 12:58
  • @Charlie thanks for your answer unfortunately I tried changing to objAtt.FileName and now my macro isn't running anymore for a reason I can't explain... – Antoine Myrd May 23 '16 at 13:52
  • Well I would start with that - the way to save all attachments is well documented and should work e.g. http://stackoverflow.com/questions/15531093/save-attachments-to-a-folder-and-rename-them – Charlie May 23 '16 at 16:09
  • I worked starting from this thread but (when it worked) it just didn't export the right format and now it doesn't work at all, I check the macros authorisations, the digital signature etc. but it doesn't seem linked to that – Antoine Myrd May 24 '16 at 08:39

1 Answers1

0

Is this what your trying to do?

Option Explicit
Public Sub SaveAtmtToDisk(Item As Outlook.MailItem)
    Dim Atmt As Outlook.Attachment
    Dim SavePath As String
    Dim FileName As String

'   //  Saved Location
    SavePath = "C:\temp\"

'   // 05 24 2016 Antoine.csv
    FileName = Format(Now, "DD MM YYYY") & " Antoine.csv"

    For Each Atmt In Item.Attachments
         Atmt.SaveAsFile SavePath & "\" & FileName
    Next

    Set Atmt = Nothing
End Sub

Tested on Outlook 2010

0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Thanks @Om3r , it's almost that, but I'd like to put the date after and I think this is what messes up the format because I also tried: `Sub saveAttachtoDisk(itm As Outlook.MailItem) Dim objAtt As Outlook.Attachment Dim saveFolder As String Dim dateFormat dateFormat = Format(Now, "dd.mm.yyyy") saveFolder = "C:\Users\etc" For Each objAtt In itm.Attachments objAtt.SaveAsFile saveFolder & "\" & "newname" & dateFormat Set objAtt = Nothing Next objAtt End Sub` But the format saves a .2016 file so i tried: & ".csv" Is it possible to combine the 2? – Antoine Myrd May 25 '16 at 08:05
  • 1
    It's all good I switched it around and found what I wanted by doing `FileName = "Antoine" & Format(Now, "DD.MM.YYYY") & ".csv" ` Thanks @Om3r and @Charlie – Antoine Myrd May 25 '16 at 08:56