Outlook macro to edit emails?

  • Thread starter Thread starter Ian Cowley
  • Start date Start date
I

Ian Cowley

I've recently transported all my mail from Eudora to Outlook, and loads of
my messages have random Eudora HTML-like tags in them that I'd like to get
rid of.
I'd like to creat (or download) a macro that will open every email I have
and strip out the tags - there are only a couple of different tags so it's a
simple search-and-replace.

I've never coded macros in VB before, so I'd like some pointers as to how I
can
a) Enumerate folders/emails and open them for editing
b) find and replace strings within the opened email.

If necessary I just need to open every email in a particular folder, as I
can manually change which folder is searched, but it's the
opening-every-email bit (or opening any email) I can;t do.

Help greatly appreciated,
 
Ian Cowley said:
I've never coded macros in Outlook before, so I'd like some pointers as to how
I can
a) Enumerate all folders/emails and open them for editing
b) find and replace strings within the opened email.

Well, I'm halfway there. I can edit all the emails in a particular folder,
using the macro below.

Now all I need is to be able to automatically cycle through every folder I
have. I also need to be able to open folders that aren't the inbox. Any
ideas?

Macro follows:
==============
Sub EditMailWithOutlook()

Dim OLF As Outlook.MAPIFolder
Dim olMailItem As Outlook.MailItem
Dim MailText As String
Dim iCnt As Integer

Set OLF = GetObject("",
"Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For iCnt = 1 To OLF.Items.Count
Set olMailItem = OLF.Items.Item(iCnt)
MailText = olMailItem.Body
MailText = Replace(MailText, <replace>, <find>, 1, -1,
vbTextCompare)
olMailItem.Body = MailText
Next iCnt

MsgBox "Done"

End Sub
 
Folders are accessible through code much the same way as they are displayed
in Outlook - hierarchically. It all starts from the top, the NameSpace
object. This is the root of your default message store, either your
Exchange Mailbox or your Personal Folders File (.pst). The NameSpace object
has a Folders collection of Folder objects, and each Folder object has a
Folders collection. You have to walk through the hierarchy to get at the
folder you need. Below is some code that you can paste into a new module.
Run it step by step, with watches on various objects to see how it works;
all folder names will be stored in the strPaths array.

Also, if you knew the EntryID value of a Folder, you can return it easily
via the NameSpace.GetFolderFromID method.

Option Explicit
Dim I As Integer
Dim strPaths() As String
Dim myNs As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim colFolders As Folders
Dim colFolders2 As Folders

Sub Run()
Set myNs = Application.GetNamespace("MAPI")
Set colFolders = myNs.Folders
RecurseFolders colFolders
End Sub

Sub RecurseFolders(objTheseFolders As Outlook.Folders)
For Each myFolder In objTheseFolders
Debug.Print myFolder.Name
ReDim Preserve strPaths(I + 1)
strPaths(I) = myFolder.FolderPath
Set colFolders2 = myFolder.Folders
I = I + 1
RecurseFolders colFolders2
Next
End Sub
 
Back
Top