• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

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-
0
MoeMoe7
Asked:
MoeMoe7
  • 6
  • 5
1 Solution
 
Chris BottomleyCommented:
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
 
Chris BottomleyCommented:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 BottomleyCommented:
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 BottomleyCommented:
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 BottomleyCommented:
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
 
Chris BottomleyCommented:
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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now