我想使用此代码段获得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

当运行此宏我得到的错误

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

User-defined type not defined

我是否需要添加任何引用来解决这件事?

有帮助吗?

解决方案

@Kusleika,我想你曾建议的选项,还是一样的错误,走了过来。 感谢您的帮助

可能是我没有措辞,我的问题的正确方法

一些更多的谷歌搜索显示,其不可能得到一个邮件的预览窗格中选中的文本。 http://www.eggheadcafe.com/forumarchives/outlookprogram_VisualBasica/Aug2005/post23481044。 ASP

所以我不得不调整的要求,这样我可以从邮寄窗口做一个动作。

下面的代码帮我(不得不做出一些改变,以适合我的需要)

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 

来源: http://www.outlookcode.com/threads.aspx ?forumid = 4&的MessageID = 26992

@Kusleika感谢您的帮助,我可以关闭此线程。请让我知道。

其他提示

万一有人使用这个词编辑器而不是HTML,还可以插入此部分:

     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

得到类似的,当字编辑器。我试图粘贴到上公认的答案评论,但它破坏了格式化,是好看不中用,所以张贴作为一个答案。

  Dim oText As Range

的TextRange是TextFrame对象的属性。它返回一个Range对象。没有TextRange对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top