打开邮件并选择文本

时间:2012-11-28 12:17:19

标签: vba outlook-vba

我已使用此代码在新窗口中打开所选消息:

  

Dim olApp As Outlook.Application

  Dim Msg As Object

  设置olApp = Outlook.Application

  设置Msg = olApp.ActiveExplorer.Selection.Item(1)

  Msg.Display

我现在想去身体并在体内选择“ERROR”文字,然后留待手工治疗。

我实际上知道这个文本出现的行号(有更多的代码)。但我的问题是如何到达消息正文,转到行,选择文本 - 然后离开例程。

1 个答案:

答案 0 :(得分:0)

尝试使用Word。未经测试的代码

Sub SearchString()
Dim myInspector As Outlook.Inspector
Dim myObject As Object
Dim myItem As Outlook.MailItem
Dim myDoc As Word.Document

Dim strItem As String

Set myInspector = Application.ActiveInspector
Set myObject = myInspector.CurrentItem

'The active inspector is displaying a mail item.
If myObject.MessageClass = "IPM.Note" And _
    myInspector.IsWordMail = True Then
    Set myItem = myInspector.CurrentItem
    'Grab the body of the message using a Word Document object.
    Set myDoc = myInspector.WordEditor
    myDoc.Range.Find.ClearFormatting
    Set mySelection = myDoc.Application.Selection
    With mySelection.Find
        .Text = "ERROR"
    End With
    If mySelection.Find.Execute = False Then
       MsgBox "There is no ERROR in this message."
    End If
End If
End Sub

改编自http://blogs.msdn.com/b/officedevdocs/archive/2011/03/15/how-to-search-for-a-string-in-an-email-message-and-automate-a-reply-that-contains-the-string.aspx

相关问题