宏VBA获取Outlook 2003中选定的文本

发布于 2024-08-27 23:14:26 字数 1010 浏览 4 评论 0原文

我正在尝试使用此代码片段在 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

“我需要添加任何引用来修复此问题吗?”

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-09-03 23:14:26

@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 感谢您的帮助,我可以关闭此线程吗?请告诉我。

@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.

梦魇绽荼蘼 2024-09-03 23:14:26

万一有人使用 word 编辑器而不是 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

以在 word 是编辑器时获得类似的效果。我尝试将其粘贴到对已接受答案的评论中,但它破坏了格式并且非常无用,因此将其作为答案发布。

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.

人生戏 2024-09-03 23:14:26
  Dim oText As Range

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

  Dim oText As Range

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文