【问题标题】:Open .rtf Attachment and Paste Contents in Current Email Body在当前电子邮件正文中打开 .rtf 附件并粘贴内容
【发布时间】:2012-06-12 21:29:10
【问题描述】:

我们有一个 Access 数据库,它使用 SendObject 方法将报告导出为电子邮件的附件。

我需要做的是打开附件,复制文本(带格式)并将其粘贴到生成的电子邮件正文中并删除文件。

我有删除附件并打开它的代码,但我不知道如何复制 Word 文档中的所有内容并将其粘贴回原始电子邮件。

任何帮助将不胜感激!如果有更简单的方法,请告诉我。

Sub olAttachmentStrip()
  Dim strFilename As String
  Dim strPath As String
  Dim olItem As Outlook.MailItem
  Dim olAtmt As Outlook.Attachments
  Dim olInspector As Outlook.Inspector
  Dim appWord As Word.Application
  Dim docWord As Word.Document

  strPath = "C:\temp\"

  Set olInspector = Application.ActiveInspector
  If Not TypeName(olInspector) = "Nothing" Then
    If TypeName(olInspector.CurrentItem) = "MailItem" Then
        Set olItem = olInspector.CurrentItem
        Set olAtmt = olItem.Attachments
            olAtmt.Item(1).SaveAsFile strPath & olAtmt.Item(1).DisplayName
            strFilename = strPath & olAtmt.Item(1).DisplayName
            'olAtmt.Item(1).Delete
    Else
    MsgBox "Something went horribly wrong."
    End If
  End If

  Set appWord = CreateObject("Word.Application")
  appWord.Visible = False 
  Set docWord = appWord.Documents.Open(strFilename)
  Stop  '<==  This is where I'm stuck!
  Set docWord = Nothing
  Set appWord = Nothing
End Sub

【问题讨论】:

    标签: vba ms-access outlook automation


    【解决方案1】:

    因为您已经有了提取附件的代码。下一步是简单地打开文件,复制完整的文本并将其粘贴到当前电子邮件中。

    试试这个(尝试和测试

    Option Explicit
    
    Sub Sample()
        Dim doc As Object, sel As Object
        Dim oWord As Object, oDoc As Object, wRng As Object
    
    
        '~~> Establish an EXCEL application object
        On Error Resume Next
        Set oWord = GetObject(, "Word.Application")
    
        '~~> If not found then create new instance
        If Err.Number <> 0 Then
            Set oWord = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        '~~> Open the Attachement
        Set oDoc = oWord.Documents.Open(FileName:="C:\MyDocument.rtf", ConfirmConversions:=False, _
            ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
            PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
            WritePasswordTemplate:="", Format:=0, XMLTransform:="", _
            Encoding:=1200)
    
        '~~> Get the comeplete text and copy it
        Set wRng = oDoc.Range
        wRng.Copy
    
        '~~> Close word Doc
        oDoc.Close
    
        '~~> Paste it in active email
        Set doc = ActiveInspector.WordEditor
        Set sel = doc.Application.Selection
        sel.Paste
    
        '~~> Clean up
        Set wRng = Nothing: Set oDoc = Nothing: Set oWord = Nothing
    End Sub
    

    【讨论】:

    • 效果很好,但我还有一个问题。我有删除附件的代码,打开它,复制选择并粘贴回原始电子邮件,但是当创建电子邮件(DoCmd.SendObject acReport)时,生成的电子邮件是作为纯文本生成的,我输了每当我创建支持 HTML 格式的电子邮件时所使用的格式。我难住了。任何帮助或指导将不胜感激。如果有更简单的方法来实现这一点,任何方向都会受到赞赏。提前谢谢你。
    • 我不太熟悉 MS Access。 DoCmd.SendObject acReport您使用的确切语法是什么?
    • 我面前没有它。该代码会生成一个包含格式的 .rtf 文档。每当我在新电子邮件上使用您的代码时,格式都是完美的。每当我将范围粘贴回 Outlook 中生成的纯文本电子邮件时,.rtf 选择就会丢失格式。我可以将活动电子邮件的格式更改回 Html 是有道理的,但我不确定这是否是最好的方法。我会在早上发布语法。谢谢
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多