简体   繁体   English

选择并复制带有VBA宏的Outlook电子邮件正文

[英]Selecting and copying Outlook email body with a VBA macro

I'm a beginner to VBA macros in Excel, and this is the first attempt in Outlook, but here's what I am trying to do: 我是Excel中VBA宏的初学者,这是Outlook中的首次尝试,但这是我想要做的事情:

In Outlook 2010, assign a macro to a button that, when pushed, 在Outlook 2010中,将宏分配给按钮,按下该按钮时,

  1. Gets the entire body of the active email 获取活动电子邮件的整个正文
  2. Copies the body including all formatting and html to the clipboard 将正文(包括所有格式和html)复制到剪贴板
  3. Opens a new word document 打开一个新的word文档
  4. Pastes the content of the clipboard to this word doc 将剪贴板的内容粘贴到此word文档
  5. Clears the clipboard 清除剪贴板

So far, all I have are steps 1 and 3 (and I wonder if I'm going about this the wrong way in step 1) below: 到目前为止,我只剩下下面的步骤1和3(我想知道我是否在步骤1中采用了错误的方式):

Sub pasteToWord()

    Dim activeMailMessage As Outlook.MailItem 'variable for email that will be copied.
    Dim activeBody
    Dim clearIt As String 'Intended to eventually clear clipboard.

'Code to get to the body of the active email.
    If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then _
    Set activeMailMessage = ActiveExplorer.Selection.Item(1)
    activeBody = activeMailMessage.Body
    'MsgBox activeBody
    '^This displayed what I want in plaintext form,
    'so I think im on the right track

'Code to copy selection to clipboard

'Code to open new Word doc
    Set WordApp = CreateObject("Word.Application")
    WordApp.Documents.Add
    WordApp.Visible = True

'Code to paste contents of clipboard to active word document

'Code to clear clipboard

End Sub

Any guidance to fill in the blanks above would be much appreciated. 任何填补上述空白的指导将不胜感激。

Edit: 编辑:

Here is what has come the closest so far, thanks to David Zemens. 这是迄今为止最接近的,这要归功于David Zemens。 I think I am missing some reference though, because my compiler doesn't understand "DataObject" for the ClearClipboard() function. 我想我虽然缺少一些参考资料,因为我的编译器不了解ClearClipboard()函数的“ DataObject”。 It does copy and paste into word with formatting though, as is below (though I had to comment out the last function to avoid errors): 它确实会复制并粘贴到具有格式的单词中,如下所示(尽管我不得不注释掉最后一个函数以避免错误):

Sub pasteToWord()

    Dim WordApp As Word.Application  'Need to link Microsoft Word Object library
    Dim wdDoc As Word.Document       'for these to be understood by compiler
    Dim activeMailMessage As Outlook.MailItem
    Dim activeBody As String

If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then

    'Get a handle on the email
    Set activeMailMessage = ActiveExplorer.Selection.Item(1)

    'Ensure Word Application is open
    Set WordApp = CreateObject("Word.Application")

    'Make Word Application visible
    WordApp.Visible = True

    'Create a new Document and get a handle on it
    Set wdDoc = WordApp.Documents.Add

    'Copy the formatted text:
    activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy

    'Paste to the word document
    wdDoc.Range.Paste

    'Clear the clipboard entirely:
     Call ClearClipBoard

End If

End Sub

Public Sub ClearClipBoard()
    Dim oData As New DataObject 'object to use the clipboard -- Compiler error, 
                                'I think I'm missing a reference here.

    oData.SetText Text:=Empty 'Clear
    oData.PutInClipboard 'take in the clipboard to empty it
End Sub

This method will copy the formatted text from the selected mailitem, and paste it in to word document: 此方法将从选定的mailitem 复制格式化的文本, 并将粘贴到word文档中:

Dim WordApp As Word.Application
Dim wdDoc As Word.Document
Dim activeMailMessage As MailItem

If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then

    'Get a handle on the email
    Set activeMailMessage = ActiveExplorer.Selection.Item(1)

    'Ensure Word Application is open
    Set WordApp = CreateObject("Word.Application")

    'Make Word Application visible
    WordApp.Visible = True

    'Create a new Document and get a handle on it
    Set wdDoc = WordApp.Documents.Add

    'Copy the formatted text:
    activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy

    'Paste to the word document
    wdDocument.Range.Paste

    'Clear the clipboard entirely:
     Call ClearClipBoard

End If

NOTE Clearing the clipboard entirely can be done pretty easily with a function like the one described here : 注意使用以下功能,可以很容易地完全清除剪贴板:

Public Sub ClearClipBoard() 
    Dim oData   As New DataObject 'object to use the clipboard

    oData.SetText Text:=Empty 'Clear
    oData.PutInClipboard 'take in the clipboard to empty it
End Sub 

You can use the Word object model when dealing woth item bodies. 处理所有项目正文时,可以使用Word对象模型。

Word is used as an email editor in Outlook. Word在Outlook中用作电子邮件编辑器。 The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which represents the Body of your email. Inspector类的WordEditor属性从代表电子邮件正文的Word对象模型返回Document类的实例。 See Chapter 17: Working with Item Bodies for more information. 有关更多信息,请参见第17章:使用项目实体

As you may see, there is no need to use any extra tools or classes (Clipboard and etc.). 如您所见,不需要使用任何其他工具或类(剪贴板等)。 You can copy the document using built-in mechanisms or save the document as is. 您可以使用内置机制复制文档或按原样保存文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM