Simple outlook macro help

Soldato
Joined
22 Oct 2005
Posts
2,829
Location
Moving...
Hi all,

I would like to create a macro for use in outlook. It’s very straightforward, all I need to is to run a search and replace for several keywords on the open email. I’ve found some VBA code which does the job in Word (with some tweaking to run multiple searches obviously), but I’m struggling to find a tutorial with how I can link this to outlook. Here’s what I found so far:


Code:
Dim oWord As Word.Application
Dim oDoc As Word.Document
    Set oWord = GetObject(, "Word.Application")
    If oWord Is Nothing Then
    Set oWord = CreateObject("Word.Application")
    End If
     
     oWord.Documents.Open FileName:="C:\Users\mp\Desktop\test.doc"
     oWord.Documents("test.doc").Activate

     Set oDoc = oWord.ActiveDocument


' Loop until no more items are found.
     Do
         oWord.Selection.Find.ClearFormatting
         With oWord.Selection.Find
             .Text = "XXX"
             .Execute ReplaceWith:="YYY", _
                  Replace:=wdReplaceAll, Forward:=True
         End With
         ' Exit the loop when the search is unsuccessful.
         If oWord.Selection.Find.Execute = False Then Exit Do
     Loop


Now the stuff within the loop is fine and does the search and replace, but I need to change all the bits beforehand to link to the open outlook message, rather than a word doc. The trouble I’m having is that all the tutorials are more complicated than what I need, they open multiple messages/folders, or run the macro when the email is sent, or when the email arrives in the inbox for example.

Anyone know of a bog-basic way to run the macro just on the active message?

Thanks for any help.
 
Maybe something like this? Been a while since I used VB

Code:
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
    Set olApp = CreateObject("Outlook.Application")
    End If

     Set objMail = olApp.ActiveInspector.CurrentItem


' Loop until no more items are found.
     Do
         olApp.Selection.Find.ClearFormatting
         With olApp.Selection.Find
             .Text = "XXX"
             .Execute ReplaceWith:="YYY", _
                  Replace:=wdReplaceAll, Forward:=True
         End With
         ' Exit the loop when the search is unsuccessful.
         If olApp.Selection.Find.Execute = False Then Exit Do
     Loop
 
Maybe something like this? Been a while since I used VB

Code:
         olApp.Selection.Find.ClearFormatting

Thanks for the help AMMUT but it's failing on that line saying; "Object doesn’t support this property or method.". Tried replacing olApp with objMail but that doesn't work either.

This is the problem I'm having - understanding all these declarative expressions at the top of the code.
 
Yeah I see the problem. Those are meant for word docs so it probably doesn't apply to an email message. Will have another look at it over lunch.
 
Try this:

Code:
Dim myInspector As Outlook.Inspector
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Dim wDoc As Word.Document
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
    Set olApp = CreateObject("Outlook.Application")
    End If

     Set myInspector = Application.ActiveInspector
	 Set objMail = myInspector.CurrentItem
	 Set wDoc = myInspector.WordEditor

' Loop until no more items are found.
     Do
		 wDoc.Selection.Find.ClearFormatting
         With wDoc.Selection.Find
             .Text = "XXX"
             .Execute ReplaceWith:="YYY", _
                  Replace:=wdReplaceAll, Forward:=True
         End With
         ' Exit the loop when the search is unsuccessful.
         If wDoc.Selection.Find.Execute = False Then Exit Do
     Loop

It uses Word after inspecting the Outlook item.

A quick search provided this link, which is where I got the idea http://blogs.msdn.com/b/officedevdo...utomate-a-reply-that-contains-the-string.aspx
 
Back
Top Bottom