views:

33

answers:

2

I am trying to use this code snippet to get the selected text in outlook 2003

Sub SelectedTextDispaly()

    On Error Resume Next
      Err.Clear

      Dim oText As TextRange

      ''# Get an object reference to the selected text range.
      Set oText = ActiveWindow.Selection.TextRange

      ''# Check to see whether error occurred when getting text object
      ''# reference.
      If Err.Number <> 0 Then

         MsgBox "Invalid Selection. Please highlight some text " _
            & "or select a text frame and run the macro again.", _
            vbExclamation
         End

      End If

      ''# Display the selected text in a message box.
      If oText.Text = "" Then
         MsgBox "No Text Selected.", vbInformation
      Else
         MsgBox oText.Text, vbInformation
      End If

End Sub

When running this macro I get the error

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

User-defined type not defined

Do I need to add any references to fix this up?

A: 
  Dim oText As Range

TextRange is a property of the TextFrame object. It returns a Range object. There is no TextRange object.

Dick Kusleika
A: 

@Kusleika, I tried the option you had suggested and still the same errors came up. Thanks for the help

May be I had not phrased my question in the proper way

Some more googling revealed that its not possible to get the selected text of a mail in preview pane. http://www.eggheadcafe.com/forumarchives/outlookprogram_VisualBasica/Aug2005/post23481044.asp

So I had to adjust the requirement so that I can do an action from an mail item window.

The following code helped me (had to make some changes to suit my needs)

Sub Blue_Code_Highlight()
    Dim msg As Outlook.MailItem
    Dim insp As Outlook.Inspector

    Set insp = Application.ActiveInspector
    If insp.CurrentItem.Class = olMail Then
        Set msg = insp.CurrentItem
        If insp.EditorType = olEditorHTML Then
            Set hed = msg.GetInspector.HTMLEditor
            Set rng = hed.Selection.createRange
            rng.pasteHTML "<font style='color: blue; font-family:Times New Roman; font-size: 10pt;'>" & rng.Text & "</font><br/>"
        End If
    End If
    Set insp = Nothing
    Set rng = Nothing
    Set hed = Nothing
    Set msg = Nothing
End Sub 

Source:http://www.outlookcode.com/threads.aspx?forumid=4&amp;messageid=26992

@Kusleika thanks for the help, can I close this thread. Pls let me know.

balalakshmi