Inserting text into a received email body using VBA in Outlook (2003)

I am trying to create a VBA program for Outlook 2003 that would take a single highlighted email message and insert a text from a string variable into the main body of the email.

Is that even possible?

Thanks,
-MOE-
MoeMoe7Asked:
Who is Participating?
 
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
Just possibly .. I am not an expert as such but since I handle the case for HTML by relacing the <BODY> keyword to ensure correct placement without disrupting the rest of the mail body.

Possibly in this case there is additional text ... can you examine the htmlbody of the email and upload the bit where it introduces <BODY making sure you remove anything sensitive first?

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Yes, perfectly feasible on received mails .... how do you want the macro to be triggered?

Chris
0
 
MoeMoe7Author Commented:
Hi Chris,

Thanks for your reply,

Actually,  this is just a small part of a larger program. The macro will get triggered when a button is pressed on the menu bar.

I will check that only one message is chosen, and that it is an email message.  Then I will be inserting the text, which I will get from a string.  Afterwards, I will be saving the email itself to a specified directory on the hard drive, and then moving the Outlook email to a "Processed" folder.

But, I am stumbling on how to insert the text into the body:  the inserted text will look like this actually:
"
*********************************
Processed on DATE
Processed by NAME
Saved to LOCATION
Attachments:  File1, File2, File3, File4......
*********************************
"
But again, the date, name, file names and their extraction are done, I just can't seem to insert the darn text!

Thanks for you help.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Chris BottomleySoftware Quality Lead EngineerCommented:
Where does the inserted text go (start or end of the current text) and can we assume the mail is text format?  Can I assume a variable exists for each of the parameters i.e. DATE, NAME, LOCATION, file1, file2?

In re the file names are they one variable or a number of possible variables

Chris
0
 
MoeMoe7Author Commented:
Hi Chris,

Good questions!

The variable does exist.  basically it is a concatenation of several variables.  For the sake of this exercise lets just say it's a single variable that has been pre-poplulated.  The inserted text should go at the beginning of the email body.

As for the text format, I would like to retain the formating in as much as the original presentation as possible, BUT if HTML  format gets too sticky to do, we can convert it to text I suppose.

-MOE-
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Formatting.  Potentially need to consider, plain text, rich text and html.  I suggest you progress plain text here and if you want to add the additional considerations then we can address them possibly later ... not sure about RTF editing just at the moment .. but someone will!

Chris
0
 
MoeMoe7Author Commented:
Sure, lets go plain text! thanks!

-MOE-
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
As an example ... addressing everything but RTF then:

Sub addBody()
Dim strText As String
Dim strHTML As String

    strText = "*********************************" & vbCrLf & _
                "Processed on DATE" & vbCrLf & _
                "Processed by NAME" & vbCrLf & _
                "Saved to LOCATION" & vbCrLf & _
                "Attachments:  File1, File2, File3, File4......" & vbCrLf & _
                "*********************************" & vbCrLf
    strHTML = Replace(strText, vbCrLf, "<BR>")
    With ActiveExplorer.Selection(1)
        If .BodyFormat = olFormatPlain Then
            .Body = strHTML & vbCrLf & ActiveExplorer.Selection(1).Body
        ElseIf .BodyFormat = olFormatRichText Then
        ElseIf .BodyFormat = olFormatHTML Then
            .HTMLBody = Replace(.HTMLBody, "<BODY>", "<BODY>" & "<BR>" & strHTML & "<BR>")
        End If
    End With
    
End Sub

Open in new window


Chris
0
 
MoeMoe7Author Commented:
Chris,

That seems to work, but wouldn't you know it, I tested it on an email from gmail and it is Richtext!  sheesh (At least I THINK it is, but I have not yet checked it out completely)

I will fiddle with it some more and keep you posted.

How could rich text be too different from normal text?  Isn't it just a bunch of additional tags here and there?

-MOE-
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
I believe you are right ... the problem as I understand is the availability of editors that handle rich text ... the use of richtext it is in point of fact something that I think even MS advise against.

Chris
0
 
MoeMoe7Author Commented:
Actually,

It looks like I was wrong about the assumption that the gmail was in Richtext.  it seems to be in HTML.  This time I stepped through the code, and I clearly saw that it skipped the Rich text and executed the HTML instead..

BUT,

although it gave me no error, it does not seem to have added any text to the email message.
I checked to make sure that the variable strHTML did contain text (and it does).

Any suggestions?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.