2017-05-26 11 views
1

使用Microsoft Outlook,我試圖更改特定附件的名稱,當用戶單擊新的Outlook電子郵件中的「發送」按鈕。使用VBA更改特定附件的名稱時發送按鈕它擊中

如果找到名稱附件,則會將該附件的名稱更改爲電子郵件的主題。

在下面的示例中,我使用「form.pdf」作爲目標附件。

當我運行代碼並嘗試更改DisplayName時,它不會更改電子郵件中實際附件的名稱。任何建議?

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    Dim myAttachments As Outlook.Attachments 
    Set myAttachments = Item.Attachments 

    For Each objAtt In myAttachments 

     If LCase(objAtt.DisplayName) = "form.pdf" Then 
      objAtt.DisplayName = Item.Subject & ".pdf" 
     End If 

    Next 
End Sub 
+0

您可能必須保存它,然後重新連接文件 – 0m3r

回答

0

Attachment.DisplayName財產絕對沒有,因爲Outlook 2002中 - 這是出於安全目的而進行的,以防止人們展示Evil.exeReadMe.txt。而Attachment.FileName屬性是隻讀的。

從理論上講,都需要需要做的是設置PR_ATTACH_LONG_FILENAME屬性(DASL名稱http://schemas.microsoft.com/mapi/proptag/0x3707001F),但是如果你使用Attachment.PropertyAccessor.SetProperty設置PR_ATTACH_LONG_FILENAME Outlook將引發異常。

可以使用擴展MAPI來設置屬性,但只能在C++或Delphi中訪問,而不能在VBA或任何.Net語言中訪問。您可以使用Redemption設置它在VBA或任何.NET語言:

set rSession = CreateObject("Redemption.RDOSession") 
rSession.MAPIOBJECT = Application.Session.MAPIOBJECT 
Item.Save 
set rItem = rSession.GetRDOObjectFromOutlookObject(Item) 
set attach = rItem.Attachments(1) 
'PR_ATTACH_LONG_FILENAME_W 
attach.Fields("http://schemas.microsoft.com/mapi/proptag/0x3707001F") = "whatever.pdf" 
'PR_ATTACH_FILENAME_W 
attach.Fields("http://schemas.microsoft.com/mapi/proptag/0x3704001F") = "whatever.pdf" 
'PR_DISPLAY_NAME_W 
attach.Fields("http://schemas.microsoft.com/mapi/proptag/0x3001001F") = "whatever.pdf" 
rItem.Save 
+0

不幸的是,這是唯一直接的方式,但它有一定道理。這次真是萬分感謝。 –