Select and copy an Outlook email body using a VBA macro

I am new to VBA macros in Excel, and this is the first attempt in Outlook, but here is what I am trying to do:

In Outlook 2010, assign a macro to a button that, when clicked,

  • Returns all text of the active message
  • Copy body including all formatting and html to clipboard
  • Opens a new document with text
  • Paste the contents of the clipboard into this word doc
  • Clears the clipboard

So far, all I have is steps 1 and 3 (and I wonder if I am not mistaken in this way below):

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 for filling the gaps above would be greatly appreciated.

Edit:

That's what came closest, thanks to David Zemens. I think that I am missing the link because my compiler does not understand the "DataObject" for the ClearClipboard () function. It copies and pastes the formatting into the word, though, as shown below (although I had to comment on 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 
+1
source share
2 answers

This method will copy the formatted text from the selected mail file and paste it into the word document:

 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 can be done quite easily using a function similar to that 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 
+3
source

You can use the Word object model when working with subject bodies.

Word is used as an email editor in Outlook. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model that represents the Body of your message. See Chapter 17: Working with Subject Elements for more information .

As you can see, there is no need to use any additional tools or classes (clipboard, etc.). You can copy the document using the built-in mechanisms or save the document as is.

0
source

Source: https://habr.com/ru/post/1300677/


All Articles