4

My question

How can get the currently selected text in Microsoft Outlook 2007

  • in an open message window
  • or in the currently selected message in the preview pane

and copy it into the clipboard?

I am using Microsoft Outlook 2007.


What I already tried

I know that I can use DataObjects to put text into the clipboard or get text from the clipboard.

The difficulty is to get the currently selected text

I tried the following commands:

  • Outlook.copy
  • Outlook.selection.copy
  • Outlook.activeexplorer.selection.item(1). ...
    in many variations, but with no success.

I found some sources stating that it would not be possible to get the currently selected text in Outlook via VBA but I think it must at least be possible to invoke a Ctrl-C (copy) command and then use the content of the clipboard.

I also stumbled upon some solutions with the Inspector as described here, but I got an error message. Maybe it is because I forgot to reference the "MS Word Object Library" - I'll try that on Monday.

  • 1
    What have you tried already? What were the results? Where are you getting stuck exactly? – Ƭᴇcʜιᴇ007 Aug 1 '14 at 16:42
  • 1
    Thank you for your comment - sorry! I was really under pressure and in a hurry, but that's not the right and polite way to ask a question here. I've edited my question and would be thankful for removing the downvote. Thanks as well for your detailed reply - I'll try that out on Monday, when I have access to the computer again. – MostlyHarmless Aug 1 '14 at 19:01
5

Since Outlook uses Word (if installed) as its editor, it's a bit of a pain to get to get it. :)

You have to reference the Word application, then the Word document, then the selection.

For the following code to work in Outlook you need to add a Reference (VBA Editor -> Tools -> References) to the "Microsoft Word Object Library", and one to the "Microsoft Forms Object Library", so that we can actually copy the text the system clipboard.

Note: For me, in Office 2013, the MS Forms object wasn't in the list, so I just added a UserForm to the VBA project, which added the reference automatically, and then just deleted the form. The reference stuck.

Anyhow I dug around many sources and here's some code I stuck together that will copy the highlighted text to the clipboard, with some basic error checking (tested in Outlook 2013):

Public Sub CopyTextToClipBoard()

    Dim objItem As Object
    Dim objInsp As Outlook.Inspector

    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next

    ' Reference the current Outlook item
    Set objItem = Application.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection

                On Error GoTo NotText
                With New MSForms.DataObject
                    .SetText objSel.Text
                    .PutInClipboard
                End With
                On Error Resume Next       

            End If
        End If
    End If

    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing

NotText:
    If Err <> 0 Then
        MsgBox "Data on clipboard is not text."
    End If

End Sub
  • 1
    thanks, it works fine - even if the text is selected in the preview pane (Outlook 2007)! (Sorry for my late feedback) – MostlyHarmless Apr 17 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.