【问题标题】:Outlook olMailItem.Attachments.Add - does an attachment have to be on the filesystem?Outlook olMailItem.Attachments.Add - 附件必须在文件系统上吗?
【发布时间】:2011-04-07 13:02:00
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    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 窗口),但存在一些注意事项:文档必须有相应的文件 - 它不适用于从未保存过的新文档,并且收件人第一次从电子邮件中打开附件时,可能会弹出一条关于开始审核过程的消息。

    希望对你有帮助,

    最大。

    【讨论】:

    • 其实,我真的不关心 .MailSend 生成的弹出窗口。但是,您的警告正是我不希望做的——通过填写一些字段生成文件,然后需要将其保存到磁盘,然后再将其作为文档附加。我还需要指定邮件的内容并简单地附加文档,所以我想我会通过将其保存到临时文件,附加它,然后在完成后将其删除。谢谢!
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      不过,奇怪的是(请参阅Debug.Print)To 文档中的 Attachments Count 并没有改变,即使内联图像都在它们应该在的位置并且可以看到和发送。玩得开心! (示例中的.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
      

      【讨论】:

        猜你喜欢
        • 2015-09-13
        • 2012-10-30
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        相关资源
        最近更新 更多