Outlook olMailItem.Attachments.Add - 附件是否必须位于文件系统上?

发布于 2024-09-17 11:55:48 字数 520 浏览 7 评论 0原文

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

我在 Word 中将此称为通过填写某些表单字段自行创建然后自行打印的文档。我希望他们也通过电子邮件发送出去,但不需要所创建文件的永久副本。我确实意识到我可以将它们保存到临时目录中,附加保存的文件,然后在发送邮件对象后删除该文件。不过这样做似乎有点浪费。

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

Is there a way to add an attachment to an email you're sending out without the attachment being on the filesystem? From reading over the DOM (http://msdn.microsoft.com/en-us/library/bb175153%28office.12%29.aspx) it says that the attachment source can be either a file path or "an Outlook item that constitutes the attachment". I don't use VBA of the office DOMs all that much and I'm not sure what this is. In addition, all the examples I can find are only giving usage examples by using the filesystem path.

I'm calling this from Word for documents that create themselves by filling out some form fields, then print themselves. I'd like them to go out via E-mail as well, but have no need for a permanent copy of the created file. I do realize that I could save them off to a temp directory, attach the saved file, then delete the file once the mail object is sent. It seems a waste to do this though.

Is there an way I can have Word pass the in-memory Document object off to Outlook to attach to the email?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果相关附件源是内联项目(例如嵌入图像),这应该适合您。我还没有尝试过使用非内联的附加文件,但它可能也可以在那里工作。基本思想是:

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

2) 使用Word 的复制和粘贴功能通过剪贴板将所有内容随身携带,因为这是一种经过充分测试的方法。在示例中,我已将新部分粘贴到新段落的开头,但您显然可以将其放置在您想要的任何位置。

但奇怪的是,(请参阅 Debug.Print)“收件人”文档中的附件计数没有改变,即使内联图像都位于它们应该在的位置并且可以看到和发送。享受 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

This should work for you, if the relevant attachment source is an inline item, such as an embedded image. I have not tried it with attached files that are not inline but it may work there too. The basic ideas are:

1) Treat the content of the Emails as a Word document because the native Editor for Outlook is Word.

2) Use Word's Copy and Paste to carry everything around with you via the Clipboard because it is a well tested approach. In the example, I have pasted the new section in at the start in a new Paragraph but you could obviously place it anywhere you want.

The strange thing is, though, (see the Debug.Print) that the Attachments Count in the To document does not change, even though the inline images are all where they should be and can be seen and Sent. Have Outlook fun! (The .olm files in the example are simply Outlook.MailItems that have been saved as Template files. They could just as easily be MailItems from an Outlook folder.)

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
北斗星光 2024-09-24 11:55:55

答案是否定的,如果不先将内存中的文档保存到磁盘,则无法将其附加到 Outlook 邮件项目。

The answer is no, you cannot attach an in-memory document to an outlook mailitem without saving it to disk first.

一梦浮鱼 2024-09-24 11:55:55

Oesor,

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

 ActiveDocument.HasRoutingSlip = True   'Creates RoutingSlip
 With ActiveDocument.RoutingSlip
   .Subject = "email Subject"
   .AddRecipient = "[email protected]"
   .Delivery = wdAllAtOnce
 End With
 ActiveDocument.Route

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

ActiveDocument.SendForReview "[email protected]", "email subject", False, True

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

我希望这有帮助,

麦克斯。

Oesor,

I made an assumption that email should get sent automatically, so SendMail method is out.
I tried a couple of things to see whether it would work. In both cases code would be embedded in your Word file.
In pre-2007 Word you could use RoutingSlip functionality:

 ActiveDocument.HasRoutingSlip = True   'Creates RoutingSlip
 With ActiveDocument.RoutingSlip
   .Subject = "email Subject"
   .AddRecipient = "[email protected]"
   .Delivery = wdAllAtOnce
 End With
 ActiveDocument.Route

Apparently, this code doesn't work in Word 2007. So instead you can use SendForReview functionality:

ActiveDocument.SendForReview "[email protected]", "email subject", False, True

Email gets sent right away (w/o the popup Outlook window), but a couple of caveats exist: the document has to have a corresponding file - it won't work for a new document that has never been saved, and the first time the recipient opens the attached document from e-mail there may be a popup message about starting the review process.

I hope this helps,

Max.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文