使用VBA保存附件

时间:2019-06-07 09:04:48

标签: vba outlook attachment

当我使用下面的VBA代码保存附件时,一切正常,但是excel文件已损坏。在Outlook中查看时文件大小为10MB,一旦移动到文件夹后它说文件大小为34KB,在尝试打开文件时出现以下错误:

“ Excel无法打开文件“ HAVERING DATA DUMP.xlsx”,因为文件格式或文件扩展名无效。请验证文件未损坏且文件扩展名与文件格式匹配”

任何帮助都将受到欢迎。

戴夫

我运行的测试文件要小得多,将文件扩展名更改为xls,但结果相同。

 Sub MoveItems()
 Dim myNameSpace As Outlook.NameSpace
 Dim myInbox As Outlook.Folder
 Dim myDestFolder As Outlook.Folder
 Dim myItems As Outlook.Items
 Dim myItem As Object
 Dim myAttachments As Outlook.Attachments
 Dim saveFolder As String

 saveFolder = "G:\Responsive\1.5 - Reports\Daily 
 Reports\OneServe\Downloads"

 Set myNameSpace = Application.GetNamespace("MAPI")
 Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
 Set myItems = myInbox.Items
 Set myDestFolder = myInbox.Folders("DATA DUMPS").Folders("HAVERING")
 Set myItem = myItems.Find("[SUBJECT] = 'HAVERING DATA DUMP'")


 Set myAttachments = myItem.Attachments
    Debug.Print myAttachments.Item(1).Size

    today = Format(myItem.ReceivedTime, "dd-mm-yyyy")
    myAttachments.Item(1).SaveAsFile saveFolder & "\" & "HAVERING DATA 
    DUMP.xlsx"
    myItem.Move myDestFolder


  End Sub

0 个答案:

没有答案