使用Outlook中的宏保存附件到电子邮件(Save attachement to email using macro in outlook)

编程入门 行业动态 更新时间:2024-10-28 05:13:32
使用Outlook中的宏保存附件到电子邮件(Save attachement to email using macro in outlook)

我想创建一个运行以下脚本的规则:

Public Sub SaveToDisk(itm As Outlook.MailItem) Dim objAtt As Outlook.Attachment Dim saveFolder As String Dim dateFormat dateFormat = Format(Now, "yyyy-mm-dd") saveFolder = "c:\temp\" objAtt.SaveAsFile saveFolder & "\" & dateFormat & ".xls" Set objAtt = Nothing End Sub

这应该保存到c:\ temp文件夹的附件,但它不起作用,因为objAtt值什么都没有。 由于某些原因,该行:

Dim objAtt As Outlook.Attachment

不会将附件分配给obAtt变量,原因我无法弄清楚。 这里的附件是.csv文件。

我在Windows 10上使用Outlook 2016。我很乐意就此为何发生任何建议。

I would like to create a rule which run the following script:

Public Sub SaveToDisk(itm As Outlook.MailItem) Dim objAtt As Outlook.Attachment Dim saveFolder As String Dim dateFormat dateFormat = Format(Now, "yyyy-mm-dd") saveFolder = "c:\temp\" objAtt.SaveAsFile saveFolder & "\" & dateFormat & ".xls" Set objAtt = Nothing End Sub

This should save the attachment to the c:\temp folder but it does not work because objAtt value is nothing. For some reasons the line:

Dim objAtt As Outlook.Attachment

does not assign the attachment to the obAtt variable for a reason I can't figure it out. Here the attachment is a .csv file.

I am using outlook 2016 on windows 10. I would be glad to get any advice on why this is happening.

最满意答案

你必须设置对象,因为你只是创建它,而不是填充它。

所以它不知道它应该与MailItem有任何关系。

此外,还有一系列的Attachement可以通过,请参阅:

Public Sub SaveToDisk(ItM As Outlook.MailItem) Dim oAttS As Outlook.Attachments Dim objAtt As Outlook.Attachment Dim saveFolder As String Dim dateFormat dateFormat = Format(Now, "yyyy-mm-dd") saveFolder = "c:\temp\" Set oAttS = ItM.Attachments For Each objAtt In oAttS objAtt.SaveAsFile saveFolder & "\" & dateFormat & "_" & objAtt.FileName Next objAtt Set oAttS = Nothing Set objAtt = Nothing End Sub

You have to set the object, because you where just creating it, not filling it.

So it had no clue it was supposed to be in any relation with the MailItem.

Further more, there is a collection of Attachement to go through, see :

Public Sub SaveToDisk(ItM As Outlook.MailItem) Dim oAttS As Outlook.Attachments Dim objAtt As Outlook.Attachment Dim saveFolder As String Dim dateFormat dateFormat = Format(Now, "yyyy-mm-dd") saveFolder = "c:\temp\" Set oAttS = ItM.Attachments For Each objAtt In oAttS objAtt.SaveAsFile saveFolder & "\" & dateFormat & "_" & objAtt.FileName Next objAtt Set oAttS = Nothing Set objAtt = Nothing End Sub

更多推荐

本文发布于:2023-07-14 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:附件   电子邮件   Save   Outlook   macro

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!