开发者

Macro VBA to get selected text in Outlook 2003

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?


@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&messageid=26992

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


Just in case someone is using the word editor instead of html, you can also insert this part:

     If insp.EditorType = olEditorWord Then
        Set hed = msg.GetInspector.WordEditor
        Set word = hed.Application
        Set rng = word.Selection
        rng.Font.Name = "Times New Roman"
        rng.Font.Size = 10
        rng.Font.Color = wdColorBlack
    End If

to get similar when word is the editor. i tried to paste this into a comment on the accepted answer, but it destroyed the formatting and was pretty useless, so posting as an answer.


  Dim oText As Range

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜