Log in

View Full Version : Change Subject on Outlook Mail and Forward



techumaclo
04-29-2015, 07:02 PM
Hi,
I need to create a script (I believe) to change the subject on all incoming email to "HC" and then forward to a separate email address. Can someone please help me? I have been looking online, but each VBA Script I try doesn't actually work when I put it into an Outlook rule.

Thanks!

techumaclo
04-29-2015, 07:03 PM
Also, I am using outlook 2010 and POP mail.

thanks,

gmayor
04-29-2015, 11:37 PM
The following run from a rule should do the job



Sub SendOnMessage(olItem As Outlook.MailItem)
Dim olOutMail As Outlook.MailItem
Set olOutMail = olItem.Forward
With olOutMail
.To = "someone@somewhere.com"
.Subject = "HC"
.sEnd
End With
lbl_Exit:
Exit Sub
End Sub

techumaclo
05-04-2015, 03:50 PM
Thanks!
Can you also tell me how i can add specific words to the body? For example, I want to forward a message that comes in to my account on outlook and have the body of my forward be the from address of the email I'm forwarding.

For example:
User 1 (user1 [at] gmail) sends an email to me (me[at]gmail). Then on my account (using Outlook) i want to forward the email using the above script and also have the forward say in the body: Message was sent from: User1[at]gmail

thanks,

gmayor
05-04-2015, 09:03 PM
You will need a few extra lines for that:



Sub SendOnMessage(olItem As Outlook.MailItem)
Dim olOutMail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Set olOutMail = olItem.Forward
With olOutMail
.To = "someone@somewhere.com"
.Subject = "HC"
.Display
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = "Message was sent from: " & olItem.SenderEmailAddress
.sEnd
End With
lbl_Exit:
Exit Sub
End Sub

techumaclo
05-16-2015, 10:26 AM
Thank you so much! This is great. 1 more question.

I am planning on having multiple email accounts come into my outlook and perform this script. I need to be able to create a new macro for each of the accounts, but how do i seperate them in the VBA? Does each get it's own module?

Thanks!

gmayor
05-16-2015, 09:42 PM
You only need the one script. When associated with a rule it acts on messages as they arrive that relate to the rule conditions.

techumaclo
06-04-2015, 12:27 PM
hi,
my outlook rule that utilizes this VBA script keeps failing, and outlook throws an error about that rule failing. does anyone have any suggestions?

techumaclo
06-04-2015, 01:02 PM
the error shows run time error 91

gmayor
06-04-2015, 08:57 PM
There is nothing contentious about the macro. It should not cause errors if entered as shown. The only line that needs to be changed is

.To = "someone@somewhere.com", where you need to replace the address with an actual address (though this dummy address should not cause an error).

Select a message and use the following macro to test the code and highlight where the error occurs.


Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
SendOnMessage olMsg
lbl_Exit:
Set olMsg = Nothing
Exit Sub
End Sub

techumaclo
06-10-2015, 08:15 PM
Hi,
No errors occur. any other advice?

Thanks,

techumaclo
06-11-2015, 12:55 PM
hi,
seeing this error13665

techumaclo
06-11-2015, 12:58 PM
When I click Debug, it highlights this line:

Set oRng = wdDoc.Range(0, 0)

Thanks,

gmayor
06-11-2015, 08:43 PM
Hmmmm. Replace that line with the following



Set oRng = wdDoc.Range
oRng.Collapse 1

techumaclo
06-15-2015, 09:41 PM
works now! thanks!