简体   繁体   English

保存 Outlook 邮件附件

[英]Saving Outlook mail attachment

I want to save an .xlsx attachment from an email.我想从电子邮件中保存 .xlsx 附件。

I am able to save the file but the extension is not by default .xlsx even though the email has an .xlsx attachment.我可以保存文件,但默认情况下扩展名不是 .xlsx,即使电子邮件有 .xlsx 附件。

Even if I save it as ".xlsx" OutlookMail.Attachments.Item(1).SaveAsFile path & ".xlsx" the file cannot be opened.即使我将其保存为“.xlsx” OutlookMail.Attachments.Item(1).SaveAsFile path & ".xlsx"文件也无法打开。

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("WeeklyMail")
Dim path As String


path = "G:\" & Format(Date, "DD-MM-YYYY") & "-"
For Each OutlookMail In Folder.Items        
    If OutlookMail.ReceivedTime >= Range("A1").Value Then
       title = OutlookMail.subject
       If InStr(title, "[Hello]") Then
            OutlookMail.Attachments.Item(1).SaveAsFile path
       End If
    End If
Next OutlookMail

@Helowxi, your path variable seems not to contain a file name at all: Check out [ https://msdn.microsoft.com/de-de/vba/excel-vba/articles/workbook-saveas-method-excel] Add the file name you desire to your variable, eg @Helowxi,您的路径变量似乎根本不包含文件名:查看 [ https://msdn.microsoft.com/de-de/vba/excel-vba/articles/workbook-sa​​veas-method-excel]添加您希望变量的文件名,例如

path = "G:\" & Format(Date, "DD-MM-YYYY") & "-MyFileName.xlsx"

and give it a try.试一试。

You can use OutlookMail.Attachments.Item(1).FileName to get the attachment's file name with extension您可以使用OutlookMail.Attachments.Item(1).FileName获取带有扩展名的附件文件名

The code below will get the file extension and save.下面的代码将获取文件扩展名并保存。 I used instrrev as a precaution against a filename with two periods.我使用 instrrev 作为预防带有两个句点的文件名的预防措施。

@EarlyBird2 is correct pointing out the missing filename @EarlyBird2 指出缺少的文件名是正确的

Dim attchFileName as String

Dim myFileName as String

Dim FileExt as String

myFileName = "some file"

attchFileName = OutlookMail.Attachments.Item(1).FileName

FileExt = Right(attchFileName, len( attchFileName) - instrrev(attchFileName,"."))

OutlookMail.Attachments.Item(1).SaveAsFile path & "." & myFileName & FileExt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM