Outlook olMailItem.Attachments.Add - 附件必须在文件系统上吗?

时间:2010-09-02 20:03:19

标签: email vba ms-word outlook ms-office

有没有办法在您发送的电子邮件中添加附件,而附件不在文件系统上?通过阅读DOM(http://msdn.microsoft.com/en-us/library/bb175153%28office.12%29.aspx),它表明附件源可以是文件路径,也可以是“构成附件的Outlook项目”。我没有那么多使用办公室DOM的VBA而且我不确定这是什么。另外,我能找到的所有示例都只是通过使用文件系统路径给出了用法示例。

我在Word中调用这个文件,通过填写一些表单字段来创建自己,然后打印自己。我希望他们也可以通过电子邮件发送,但不需要创建文件的永久副本。我确实知道我可以将它们保存到临时目录,附加保存的文件,然后在发送邮件对象后删除文件。尽管这样做似乎是浪费。

有没有办法让Word将内存中的Document对象传递给Outlook以附加到电子邮件?

3 个答案:

答案 0 :(得分:1)

答案是否定的,您不能将内存文档附加到outlook mailitem而不先将其保存到磁盘。

答案 1 :(得分:0)

Oesor,

我假设电子邮件应该自动发送,因此SendMail方法已经发布。  我尝试了几件事来看看它是否会起作用。在这两种情况下,代码都将嵌入到Word文件中。  在2007年之前的Word中,您可以使用RoutingSlip功能:

 ActiveDocument.HasRoutingSlip = True   'Creates RoutingSlip
 With ActiveDocument.RoutingSlip
   .Subject = "email Subject"
   .AddRecipient = "recipient@domain.com"
   .Delivery = wdAllAtOnce
 End With
 ActiveDocument.Route

显然,此代码在Word 2007中不起作用。因此您可以使用SendForReview功能:

ActiveDocument.SendForReview "recipient@domain.com", "email subject", False, True

立即发送电子邮件(没有弹出的Outlook窗口),但存在一些警告:文档必须有相应的文件 - 它不适用于从未保存过的新文档,并且收件人第一次从电子邮件中打开附加文档时,可能会有一条关于启动审阅过程的弹出消息。

我希望这有帮助,

最大

答案 2 :(得分:0)

如果相关附件源是内嵌项目(例如嵌入图像),则此选项适用于您。我没有尝试使用非内联的附加文件但可能也在那里工作。基本思路是:

1)将电子邮件的内容视为Word文档,因为Outlook的本机编辑器是Word。

2)使用Word的复制和粘贴通过剪贴板随身携带一切,因为它是一种经过良好测试的方法。在这个例子中,我在一个新的段落中粘贴了新的部分,但你可以将它放在你想要的任何地方。

但奇怪的是,(请参阅Debug.Print)To文档中的附件计数不会更改,即使内嵌图像应该是所有位置,也可以看到和发送。有趣的Outlook! (示例中的.olm文件只是已保存为模板文件的Outlook.MailItems。它们可以很容易地成为Outlook文件夹中的MailItems。)

Private Sub TestAttach()
'Places inline Attachment information into a different MailItem
Dim OlTo As Outlook.MailItem
Dim OlFrom As Outlook.MailItem
Dim DocTo As Word.Document
Dim DocFrom As Word.Document
Dim R As Word.Range
Dim R1 As Word.Range
Dim R2 As Word.Range
Dim lStart As Long
Dim lEnd As Long

Set OlFrom = Outlook.CreateItemFromTemplate("C:\Temp\OlTemplateWithSomeOtherAttachments.oft")
Set OlTo = Outlook.CreateItemFromTemplate("C:\Temp\OlTemplateWithSomeAttachments.oft")
Debug.Print "From file starts with " & OlFrom.Attachments.Count & " attachments."
Debug.Print "To   file starts with " & OlTo.Attachments.Count & " attachments."
Set DocFrom = OlFrom.GetInspector.WordEditor
Set DocTo = OlTo.GetInspector.WordEditor
OlFrom.Display
OlTo.Display

Set R2 = DocFrom.Content
With R2.Find                    'Note: Find settings are 'sticky' and do not need to be repeated on the next find.
    .Forward = True
    .Wrap = wdFindStop          'Do not loop back to the start of the document
    .Format = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Text = "Start flag for Section with Attachments"  'Find the start of the section to move
    .Execute
    lStart = R2.Start
    .Text = "End flag for Section with Attachments"    'Find the end of the section to move
    R2.Collapse wdCollapseEnd
    .Execute
    lEnd = R2.Start
End With
'OlFrom.Display
Set R2 = DocFrom.Range(lStart, lEnd)
'R2.Select
R2.Copy
Set R = DocTo.Range(1, 1)
R.InsertParagraphBefore
'Place the new inline attachments in the To MailItem
Set R = DocTo.Range(1, 1)
R.Paste
OlTo.Display
Debug.Print OlTo.Attachments.Count; "To   file ends with " & OlTo.Attachments.Count & " attachments, the same as the original number but all the inline images show."
End Sub