当点击发送按钮时,使用 VBA 更改特定附件的名称 [英] Use VBA to Change the Name of Specific Attachment when the Send Button it Hit
问题描述
使用 Microsoft Outlook,当用户单击新 Outlook 电子邮件中的发送"按钮时,我试图更改特定附件的名称.
Using Microsoft Outlook, I am trying to change the name of a specific attachment when the user clicks the "Send" button in a new outlook email.
如果找到了带有该名称的附件,则会将该附件的名称更改为电子邮件的主题.
If an attachment with the name is found then it will change that attachment's name to the subject of the email.
在下面的示例中,我使用form.pdf"作为目标附件.
In the example below, I am using "form.pdf" as the target attachment.
当我运行代码并尝试更改 DisplayName 时,它不会更改电子邮件中实际附件的名称.有什么建议吗?
When I run the code and try to change the DisplayName it doesn't change the name of the actual attachment in the email. Any Advice?
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
推荐答案
Attachment.DisplayName
属性自 Outlook 2002 起绝对没有任何作用 - 这样做是为了安全目的,以防止人们显示 Evil.exe
作为 ReadMe.txt
.而 Attachment.FileName
属性是只读的.
Attachment.DisplayName
property does absolutely nothing since Outlook 2002 - this is done for security purposes to prevent people from showing Evil.exe
as ReadMe.txt
. And Attachment.FileName
property is read-only.
理论上,所有需要做的就是设置PR_ATTACH_LONG_FILENAME
属性(DASL名称http://schemas.microsoft.com/mapi/proptag/0x3707001F
),但如果您使用 Attachment.PropertyAccessor.SetProperty
设置 PR_ATTACH_LONG_FILENAME
,Outlook 将引发异常.
In theory, all need to need to do is set the PR_ATTACH_LONG_FILENAME
property (DASL name http://schemas.microsoft.com/mapi/proptag/0x3707001F
), but Outlook will raise an exception if you use Attachment.PropertyAccessor.SetProperty
to set PR_ATTACH_LONG_FILENAME
.
您可以使用扩展 MAPI 来设置属性,但它只能在 C++ 或 Delphi 中访问,不能在 VBA 或任何 .Net 语言中访问.您可以使用 Redemption 在 VBA 或任何 .Net 语言中进行设置:
You can use Extended MAPI to set the property, but it is only accessible in C++ or Delp not in VBA or any .Net languages. You can use Redemption to set it in VBA or any .Net language:
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
这篇关于当点击发送按钮时,使用 VBA 更改特定附件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!