Auswählen und kopieren von Outlook-E-Mail-Körper mit einem VBA-makro

Ich bin ein Anfänger von VBA-Makros in Excel, und dies ist der erste Versuch, in Outlook, aber hier ist, was ich versuche zu tun:

In Outlook 2010, zuweisen eines Makros zu einer Schaltfläche, wenn diese gedrückt,

  1. Wird der gesamte Körper des aktiven E-Mail -
  2. Kopiert den Körper einschließlich aller Formatierungen und html in die Zwischenablage
  3. Öffnet ein neues word-Dokument
  4. Fügt den Inhalt der Zwischenablage in das word-doc
  5. Löscht die Zwischenablage

So weit, alles, was ich habe, sind die Schritte 1 und 3 (und ich Frage mich, wenn ich dabei bin, über das der falsche Weg, der in Schritt 1) unten:

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

Jede Anleitung füllen Sie die Felder oben wäre sehr geschätzt werden.

Edit:

Hier ist, was Sie am nächsten kommen, so weit, Dank an David Zemens. Ich glaube, ich bin fehlen einige Bezugspunkte, obwohl, weil mein compiler nicht verstehen "DataObject" für die ClearClipboard () - Funktion. Es funktioniert kopieren und einfügen in word mit Formatierung, obwohl, wie unten (obwohl ich hatte, um kommentieren Sie die Letzte Funktion, um Fehler zu vermeiden):

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
InformationsquelleAutor AnthonyJS | 2015-01-12
Schreibe einen Kommentar