Macro to format email in a certain folder then forward to email address

Status
Not open for further replies.

divan

Member
Outlook version
Outlook 2007
Email Account
POP3
Hi

I want a rule or a macro that enables me to do the following:

I receive a email from email_A@example.com
I then want to change the subject of the email to the body of the email
than forward the email to email_B@example.com

I can currently change the subject
- BUT it changes ALL my INBOX mail not just coming from email_A@example.com.

I can also forward the email
- BUT not just from the email_A@example.com it sends all my inbox to email_B@example.com



PLEASE help with my script:

Sub ChangeSubjectThenSend(item As Outlook.MailItem)
Dim myolApp As Outlook.Application
Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

For Each aItem In mail.Items
aItem.Subject = aItem.Body
aItem.Save

Set myForward = aItem.Forward
myForward.Recipients.Add "email_B@example.com"
myForward.Send

Next aItem
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is the problem - Set mail = myolApp.ActiveExplorer.CurrentFolder

if you are using this in a rule, you don't need a bunch of the lines - the (item as outlook.mailitem) part identifies the incoming message as Item - and that is all you need.

if you only want the forwarded message to have the body as the subject (and not the original message), drop the first two lines and add this before the send
myForward.Subject = Item.Body


Code:
Sub ChangeSubjectThenSend(item As Outlook.MailItem)
Item.Subject = Item.Body
Item.Save

Set myForward = Item.Forward
myForward.Recipients.Add "email_B@example.com"
myForward.Send

End Sub
 

divan

Member
Outlook version
Outlook 2007
Email Account
POP3
Hi Diane Poremsky

The above mentioned script works well thanks.

I would just to adjust it slightly so forwarded mail does not include the "FW:" in the subject and is sent with plain text formatting.

Does this look right for you?


Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim fwd as Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set fwd = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
msg.BodyFormat = olFormatPlain
Set fwd = msg.Forward
fwd.To = "email_B@example.com; email_C@example.com"
fwd.Subject = msg.Body
fwd.Send

Set msg = Nothing
Set fwd = Nothing
Set olNS = Nothing
End Sub


Thanks again for your input!
 

divan

Member
Outlook version
Outlook 2007
Email Account
POP3
Don't know why I make it so difficult for myself...
Sorted it out thanks. I did not need the formatting to be in plain text, so that made it simple.

Sub ChangeSubjectThenSend(item As Outlook.MailItem)
Item.Subject = Item.Body
Item.Save

Set myForward = Item.Forward
myForward.Recipients.Add "email_B@example.com"
myForward.Subject = item.Body
myForward.Send

End Sub

Thanks again!!
 
Status
Not open for further replies.
Top