Saving and converting attached Outlook file with VBA

时间:2016-07-28 20:22:35

标签: xml vba outlook outlook-vba xlsx

I have this code for saving the attached file from emails to a given folder. But I would like to convert these files before saving as well. Especially the xml files to xlsx.

Could anybody help me?

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   

1 个答案:

答案 0 :(得分:0)

我做到了。

Public Sub saveconvAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
Dim convFormat As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

saveFolder = "C:\Users\gabor\Documents\CAFM\xml\"
convFolder = "C:\Users\gabor\Documents\CAFM\xls\"
dateFormat = Format(Now, "yyyy-mm-dd HH-mm-ss")

For Each objAtt In itm.Attachments

objAtt.SaveAsFile saveFolder & dateFormat & objAtt.FileName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(saveFolder)
    If UCase(Right(objAtt.FileName, Len(XML))) = UCase(XML) Then
        NewFileName = convFolder & dateFormat & objAtt.FileName & "_conv.xlsx"

Set ConvertThis = Workbooks.Open(saveFolder & dateFormat & objAtt.FileName)
        ConvertThis.SaveAs FileName:=NewFileName, FileFormat:= _
        xlOpenXMLWorkbook
        ConvertThis.Close
    End If
Next
Set objAtt = Nothing

End Sub