按发件人地址保存附件

时间:2014-12-29 22:33:18

标签: vba outlook

我想将发件人地址附加到我的附件文件名。

例如,我收到了一封来自johnsmith@fakeemail.com的电子邮件,附件是somespreadsheet.xls。

我想将该文件自动保存为somespreadsheet.johnsmith.xls(或该文件名的任何变体都可以使用)。

我每天收到大约200个这样的电子表格,但它们都被命名为相同的,somespreadsheet.xls,我正在寻找一种更快/更简单的方法来将它们从电子邮件中删除,同时保持将它们发送给我

1 个答案:

答案 0 :(得分:0)

Outlook对象模型中的Attachment类提供了允许指定文件名的SaveAsFile方法。

Sub SaveAttachment() 
   Dim myInspector As Outlook.Inspector  
   Dim myItem As Outlook.MailItem  
   Dim myAttachments As Outlook.Attachments  

   Set myInspector = Application.ActiveInspector 

   If Not TypeName(myInspector) = "Nothing" Then  
      If TypeName(myInspector.CurrentItem) = "MailItem" Then  
         Set myItem = myInspector.CurrentItem  
      Set myAttachments = myItem.Attachments  

   'Prompt the user for confirmation  
   Dim strPrompt As String  
   strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."  
   If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then  
      myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _  
      myAttachments.Item(1).DisplayName  
   End If  
 Else  
   MsgBox "The item is of the wrong type."  
 End If  
End If  
End Sub

正如您所看到的,Attachment类的DisplayName属性用于指定文件名。因此,您可以将它与MailItem类的Sender *相关属性结合使用 - 请参阅SenderNameSenderEmailAddress属性。