Macro to forward mail with changed text ?

  • Thread starter Thread starter Kim
  • Start date Start date
K

Kim

Hello,

I wonder if anyone could help me.
I want to forward a piece of email with changed words in the body and
subject.

So
Find current open email
Make changes to it. Body and Subject
Take this changed text and put it into a reply or forwarded message.

I can currently get the email from the current open Email message. Change it
and put it into a variable. (although I don't know if there is a limit on
how many characters you can put into a variable, the emails are not long.)

But what I don't know how to do pragmatically, is how to open a new email
and place the newly changed message into an email. Ready for sending.

Regards

Thank You.
 
It sounds like this is all you need to do:

Sub GetTextFromOpenMessageAndPutInNewEmail()
On Error Resume Next

Dim objMail As Outlook.MailItem, objNewMail As Outlook.MailItem

If Application.ActiveInspector Is Nothing Then Exit Sub
If Application.ActiveInspector.CurrentItem.Class <> olmail Then Exit Sub

Set objMail = Application.ActiveInspector.CurrentItem

Set objNewMail = Application.CreateItem(olMailItem)
'Or, to base this on a Reply or Forward to the currently open message
' Set objNewMail = objMail.Reply
' Set objNewMail = objMail.Forward

objNewMail.Body = objMail.Body
objNewMail.Display

Set objNewMail = Nothing
Set objMail = Nothing
End Sub
 
You don't need to discard the first two objects and start over. The body of
the original message is never altered, and the Save method is never called.
Just use your Replace function against the original objMail.Body, pass it
into a variable, and pass that to objNewMail.Body.
 
Thanks. This is very helpfull.

Here is what I have done with it. Please don't be to critical of my poor
abilities.
I am self taught and only do things to make my life and those around me a
little easier.


-----------------

Sub GetTextFromOpenMessageAndPutInNewEmail3()
On Error Resume Next

Dim objMail As Outlook.MailItem
Dim objNewMail As Outlook.MailItem

Dim sSearchString As String '
Dim sReplaceString As String '
Dim sNewBody As String '

If Application.ActiveInspector Is Nothing Then Exit Sub
If Application.ActiveInspector.CurrentItem.Class <> olMail Then Exit Sub

Set objMail = Application.ActiveInspector.CurrentItem

''Set objNewMail = Application.CreateItem(olMailItem)
'Or, to base this on a Reply or Forward to the currently open message
' Set objNewMail = objMail.Reply
Set objNewMail = objMail.forward

'Open a new forwarded email
objNewMail.Display

'Now clear things out and start over on Forwarded mail
'I dont want to change original.
Set objNewMail = Nothing
Set objMail = Nothing

Set objMail = Application.ActiveInspector.CurrentItem
Set objNewMail = Application.CreateItem(olMailItem)

sBodyString = objMail.Body
sOldString1 = "a" '
sNewString1 = "x"

sNewBody = Replace(sBodyString, sOldString1, sNewString1, , ,
vbTextCompare)

objMail.Body = sNewBody


Set objNewMail = Nothing
Set objMail = Nothing

ByeBye:
End Sub
-----------------------


Regards
Kim
 
Hang on! You're almost there:

Move "objNewMail.Body = sNewBody" to after "Set objNewMail =
objMail.forward". Also, you can delete the line "objMail.forward =
objNewMail"; this will actually cause an error.
 
Once again your on the money. Thanks.
I went through the whole thing and adjusted it. This is much better and
perhaps less convolluted.

Actually I kept trying before but couldn't seem to get it right so opted for
the "Discard" method as you have entitled it.
But with your advice I persevered believeing that you knew what you were
talking about.
Thanks again.

Kim.
Here is my code again. For those like me that are trying to learn.
------------------------
Sub aTry2GetTextFromOpenMessageAndPutInNewEmailTry()
On Error Resume Next

Dim objMail As Outlook.MailItem, objNewMail As Outlook.MailItem
Dim sSearchString As String, sReplaceString As String, sNewBody As
String 'for Replace code

If Application.ActiveInspector Is Nothing Then Exit Sub
If Application.ActiveInspector.CurrentItem.Class <> olMail Then Exit Sub

Set objMail = Application.ActiveInspector.CurrentItem
sOldString1 = "a" 'Old string
sNewString1 = "k" 'New string

sNewBody = Replace(objMail.Body, sOldString1, sNewString1, , ,
vbTextCompare)
objNewMail.Body = sNewBody

' Set objNewMail = Application.CreateItem(olMailItem)
'Or, to base this on a Reply or Forward to the currently open message
' Set objNewMail = objMail.Reply
Set objNewMail = objMail.forward

objMail.forward = objNewMail

'Open a new forwarded email
objNewMail.Display



Set objNewMail = Nothing
Set objMail = Nothing
End Sub
 
Eric,
Again thank for your advice.

The problem is that when I do it as you have suggested, though it runs
perfectly, it does not give me the result I'm looking for e.g..

While in the Subject line has FW
The body of the message does not start with the standard
----------------snip ---------------------
-----Original Message-----
From: Mary Semple [mailto:[email protected]]
Sent: March 15, 2005 1:54 PM
To: 'Kimberly'
Subject: RE: Computer Needs
---------------snip ------------------
Telling me when it was forwarded etc. Instead it give the exact message it
copied accept for changes. I think that this is because its putting into the
forwarded message exactly the message that was in the original email with
the new changes. Perhaps it was my poor explanation. While I could see the
advantage in this for some for me I would like to see this forwarded message
Info.

I have removed the objMail.forward = objNewMail , which didn't cause an
error but also didn't do anything. I think it was a bit of chaff left over
from previous attempts. But when I move the objNewMail.Body = sNewBody to
below the Set objNewMail = objMail.forward I get the problem of no Forwarded
data. When I keep it where it is I get it.

Any thoughts.

Regards
 
Back
Top