-1

I try to run this code, but it does not save the xml file to the given folder. What is wrong with it?

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\gabor\Documents\CAFM\xml\"
For Each objAtt In itm.Attachments
    If InStr(objAtt.DisplayName, ".xml") Then
    objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
End If
Set objAtt = Nothing
Next
End Sub
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
vergab
  • 33
  • 2
  • 2
  • 9
  • Why the extra "\" in `saveFolder & "\" & objAtt.DisplayName` since you already have "\" in `saveFolder = "C:\Users\gabor\Documents\CAFM\xml\"` – Siddharth Rout Jul 28 '16 at 08:34
  • You are right. I deleted it, but it still doesn't work. This code is in an Outlook rule. This rule saves the attachment from the email. – vergab Jul 28 '16 at 08:57
  • Try changing `objAtt.DisplayName` to `objAtt.Filename` – Siddharth Rout Jul 28 '16 at 09:03
  • I did it, but still doesn't work. – vergab Jul 28 '16 at 09:25
  • Try using `itm.Attachments.Count` as shown in Part 2 [Here](http://stackoverflow.com/questions/11781320/download-attachment-from-outlook-and-open-in-excel/11782153#11782153) and then stepping through it. Does your code enter the relevant area? – Siddharth Rout Jul 28 '16 at 09:27
  • The moved emails always have attachment at least one. This code is in the VbbaProject.OTM-Microsoft Outlook Objects-Module3 and the outlook rule refers to this macro. – vergab Jul 28 '16 at 09:50
  • Can you step through your code and check if it actually gets executed? – Dmitry Streblechenko Jul 28 '16 at 17:01
  • Changing the security settings solved the problem. I had to set the macro settings to enable all macro. – vergab Jul 28 '16 at 20:09

1 Answers1

0

From comment "Changing the security settings solved the problem. I had to set the macro settings to enable all macro." – vergab Jul 28 at 20:09