piper1935
piper1935

Reputation: 109

Selecting Outlook Emails to Paste to Word Document - VBA Macro

I want to:

  1. Select all emails from a certain sender

  2. Copy the body of the email to a new Word document

  3. Save the word document to a specific directory

  4. Clear the clipboard

I'd like to know what else I need to do, especially in the the FindSetAside() and SaveToDicrectory() functions.

Sub FindSetAside()  'find all set-aside emails

End Sub

Sub PasteToWord()
    Dim Word As Word.Application
    Dim Doc As Word.Document
    Dim activeMessage As Outlook.MailItem 'the email to copy
    Dim activeBody As String
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
    'get the active email
    Set activeMessage = ActiveExplorer.Selection.Item(1)
    'setup Word
    Set Word = CreateObject("Word.Application")
    WordApp.Visible = True
    setDoc = Word.Documents.Add
    'Copy selection to document
    activeMessage.GetInspector().WordEditor.Range.FormattedText.Copy
    Doc.Range.Paste
    Call ClearClipboard
End If
End Sub

Sub SaveToDirectory() 'Save the Word Document to the correct directory

End Sub

Public Sub ClearClipboard()
    Dim Data As New DataObject
    Data.SetText Tex:=Empty
    Data.PutInClipboard
End Sub

I am using Outlook 2010. I am also considering adding some code to send the Word document as an attachment to specific emails, but perhaps that is irrelevant to this question.

Upvotes: 1

Views: 1347

Answers (1)

Eric Legault
Eric Legault

Reputation: 5834

Using Explorer.Selection won't help if you need to execute a search for certain emails (unless you use Explorer.Search, which executes a search in the UI). Take a look at this to help determine the search method you need to use:

https://msdn.microsoft.com/EN-US/library/ff869846.aspx

Then it is just a matter of traversing the returned collection and accessing the MailItem objects within.

To save the Word document, use Document.SaveAs2.

Upvotes: 1

Related Questions