View Full Version : New email message body

09-26-2005, 05:11 AM
Hello. I am not a noob regarding VB but regarding VBA in Outlook, I am.
I have succeeded to read message body from an email that has arrived.
What i need now is when i write new message, i want to click on a button (allready in place) and read all the text in the message body, process it as i see fit and replace the body text with the processed text.

09-26-2005, 11:13 AM
Well, that's a doozy.

In VBA, you can usually access the message body through the Body or HTMLBody properties of a MailItem object. The biggest problem I see here is getting the reference to the specific MailItem object in question. To my current knowledge, I don't know any method that will return a reference to a MailItem object from an open New Message form. You could make a reference by creating the new MailItem from a macro, but the macro would have to stop running to let the user enter the message, losing the reference in the process.

However, you could save the message into the Drafts folder before sending it, and then code a macro to process items from within the Drafts folder. That code would look something like this:

Option Explicit
Public Sub MyButton()
Dim ns as NameSpace
Dim draftsFolder As MAPIFolder
Dim itm as MailItem
Dim messageText as String
Set ns = Application.GetNamespace("MAPI")
Set draftsFolder = ns.GetDefaultFolder(olFolderDrafts)
Set ns = Nothing
For Each itm in draftsFolder.Items
messageText = itm.Body ' or itm.HTMLBody
' Do whatever you want with messageText
itm.Body = messageText ' or itm.HTMLBody
End Sub

Alternatively, you could use VBScript to alter how the New Message form works. Use the Tools/Forms/Design a Form menu option to open the Message form in the Standard Forms Library and place your button in the actual form design. Then you can reference the message text by calling Item.Body in the VBScript window, like this:

Sub NameOfButton_Click()
strMsg = Item.Body ' Returns the message typed thus far
' Do whatever you want with strMsg
Item.Body = strMsg ' Sets the message with your altered string
End Sub

Hope that helps.