2010-04-07 27 views
2

我想使用此代码段来获得在Outlook中选定的文本2003宏VBA在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 

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

回答

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感谢您的帮助,我可以关闭此线程。请让我知道。

0
Dim oText As Range 

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

1

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

得到类似的,当字编辑器。我试图将其粘贴到对已接受答案的评论中,但它破坏了格式并且非常无用,因此发布为答案。

+0

(只在Outlook 2010中尝试过) – 2011-06-17 18:04:09

相关问题