Frank Thure Lindgren
Frank Thure Lindgren

Reputation: 29

Open a message and select text

I have used this code to open the selected message in a new window:

Dim olApp As Outlook.Application

Dim Msg As Object

Set olApp = Outlook.Application

Set Msg = olApp.ActiveExplorer.Selection.Item(1)

Msg.Display

I now want to go to the body and select the text "ERROR" inside the body, and then leave it for manual treatment.

I actually know the line number where this text appears (there is som more code). But my problem is how to get to the body of the message, go to the line, select the text - and then leave the routine.

Upvotes: 2

Views: 1491

Answers (1)

niton
niton

Reputation: 9179

Try using Word. Untested code

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

Adapted from 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

Upvotes: 1

Related Questions