代码之家  ›  专栏  ›  技术社区  ›  balalakshmi

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

  •  2
  • balalakshmi  · 技术社区  · 16 年前

    我正在尝试使用此代码段获取outlook2003中选定的文本

    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
    

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

    3 回复  |  直到 16 年前
        1
  •  1
  •   balalakshmi    16 年前

    @库什莱卡,我试过你建议的方法,但还是出现了同样的错误。

    也许我的问题措辞不恰当

    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

    @谢谢你的帮助,我能合上这条线吗。请告诉我。

        2
  •  1
  •   John Gardner    15 年前

    如果有人使用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是编辑的时候,也能得到相似的结果。我试着把它粘贴到一个关于被接受答案的评论中,但是它破坏了格式,而且非常无用,所以作为一个答案发布。

        3
  •  0
  •   Dick Kusleika    16 年前
      Dim oText As Range
    

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

    推荐文章