【问题标题】:Macro VBA to get selected text in Outlook 2003在 Outlook 2003 中获取选定文本的宏 VBA
【发布时间】:2010-04-07 10:45:38
【问题描述】:

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

我需要添加任何引用来解决这个问题吗?

【问题讨论】:

    标签: vba outlook-2003


    【解决方案1】:

    @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 谢谢你的帮助,我可以关闭这个帖子吗?请告诉我。

    【讨论】:

      【解决方案2】:

      万一有人用的是word editor而不是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 是编辑器时得到相似。我试图将其粘贴到已接受答案的评论中,但它破坏了格式并且毫无用处,因此作为答案发布。

      【讨论】:

        【解决方案3】:
          Dim oText As Range
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-04
          • 1970-01-01
          • 1970-01-01
          • 2018-11-27
          • 2018-09-23
          • 2015-10-17
          • 1970-01-01
          • 2011-02-21
          相关资源
          最近更新 更多