Home > Exchange, HowTo, VbScripts > Outlook Macro to forward emails

Outlook Macro to forward emails

September 30th, 2008 Leave a comment Go to comments

.
.
I just wrote my first Outlook VBA code, it searches the body of the email for a email and forwards that email to it.
Just creat a new rule right click on the email and call it, it should also work with a rule.


Dim emailtosend As String

Sub FwdMailText()
Dim objItem As Object
Dim Msg As Outlook.MailItem
Dim NewForward As Outlook.MailItem
Dim MyFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set objItem = _
Application.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set objItem = _
Application.ActiveInspector.CurrentItem
End Select
On Error GoTo 0

If objItem Is Nothing Then
MsgBox "Nothing selected!", vbExclamation
GoTo ExitProc
End If
If objItem.Class = olMail Then
Set Msg = objItem
Else
MsgBox "No message selected!", vbExclamation
GoTo ExitProc
End If
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set MyFolder = olNS.GetDefaultFolder(olFolderInbox)
Msg.BodyFormat = olFormatPlain
'CALL THE SUB TO PARSE BODY TEXT AND GET THE EMAIL
Call SearchBody(Msg.Body)

Set NewForward = Msg.forward
With NewForward
.Subject = msg.Subject
.To = emailtosend
.Body = Msg.Body
.send
End With
With Msg
.UnRead = False
.FlagStatus = olNoFlag
End With
ExitProc:
Set NewForward = Nothing
Set Msg = Nothing
Set objItem = Nothing
End Sub

Sub SearchBody(msgbody)
lines = Split(msgbody, vbCrLf)
'--GET THE EMAIL TO SEND TO
For Each Line In lines
If InStr(Line, "Email:") Then
parsed = Split(Line, ":")
If InStr(parsed(1), "@") Then
emailtosend = parsed(1)
'MsgBox emailtosend, vbInformation
Else
'--Error Parsing
MsgBox "error parsing email", vbCritical
End If
End If
Next
End Sub

  1. October 1st, 2008 at 01:12 | #1

    If SearchBody doesn’t return the email, your routine still continues to create the forwarded mailitem. You probably want to write it as a function that returns a value; that way you can check if it was successful.

    Function SearchBody(msgbody) As Boolean
    lines = Split(msgbody, vbCrLf)
    ‘–GET THE EMAIL TO SEND TO
    For Each Line In lines
    If InStr(Line, “Email:”) Then
    parsed = Split(Line, “:”)
    If InStr(parsed(1), “@”) Then
    emailtosend = parsed(1)
    ‘MsgBox emailtosend, vbInformation
    Else
    ‘–Error Parsing
    MsgBox “error parsing email”, vbCritical
    End If
    End If
    Next
    End Function

    Then you can call it like this:

    If SearchBody(Msg.Body) Then
    ‘ continue code
    Else
    MsgBox “error parsing email”, vbCritical
    Goto ExitProc
    End If

  2. January 14th, 2009 at 07:08 | #2

    I am looking for a macro that will send me an email if my email is sent/forwarded to another email address. it would tell me who the email was forwardedto . Does this macro do this ??

  3. March 19th, 2009 at 13:58 | #3

    Tom,
    There’s no macro that can do that. It would have to be installed on the other computer, so your recipient would have to agree to report to you whenever your emails are forwarded.

  4. gitecravirs
    June 3rd, 2009 at 10:15 | #4

    i have reinstalled my net-snmp-5.4.2.1 on ubuntu-8.04 hardy( and its
    only ipv6 host, address assigned to it is 2001:468::3/64 on eth0
    interface and eth1 im using for dhcp-internet connection). Sir, i have
    new problem, my snmpd is start now and i am also getting its pid and
    port no.(as 161) but im not getting the result of snmpwalk (both for
    localhost and remotehost). i run the following commands–
    #snmpwalk -v2c -c public localhost system ; on local host
    output-timeout no response from localhost
    #snmpwalk -v2c -c public 192.168.122.3 system ; on remotehost(
    remotehost is having ip 192.168.1.2 and its ipv4 only machine. im
    doing the project ipv4 to ipv6 migration and vice versa. im using 3
    pc’s here ist is ipv6 pc(add-2001:468::3/64) , 2nd pc is ipv4 only
    (add-192.168.1.2) and 3rd one is nat-pt box which maps the ipv4
    address corresponds to the ipv6 here in the case it maps 192.168.122.3
    to the 2001:468::3/64. add. of nat-pt is 192.168.1.1)
    output-timeout no response from 192.168.122.3

    im attaching my /etc/default/snmpd , /etc/snmp/snmpd.conf and
    /usr/local/share/snmp/snmpd.conf( snmpd.conf file is same for
    /etc/snmp and /usr/local/share/snmp directories). please tel me what
    changes should i do in my default and snmpd.conf files so that my
    snmpwalk starts working. Please help me soon.

    Thanks Alot
    ————————————————-
    snmpd.conf

    • admin
      June 3rd, 2009 at 10:49 | #5

      ravi,
      I would check with netstat -an | grep :161 is SNMPD listening in the IPV6 address? when you do ping localhost, does it ping the IPV6 ?
      I have never setup snmpd with IPV6 but it should work, any iptables rule?
      Maybe try doing #snmpwalk -v 2c -c public udp6:::1 sysname
      You need to get snmpd to listen to IPv6 by editing /etc/default/snmpd so as to read:
      SNMPDOPTS=’-s -l /dev/null -P /var/run/snmpd.pid udp6:161′
      Note about 2.6.x kernels: you need to explicitly enable both ipv4 and ipv6, like this:
      SNMPDOPTS=’-s -l /dev/null -P /var/run/snmpd.pid udp6:161 udp:161′
      More info here

  5. Dean
    October 28th, 2009 at 18:24 | #6

    Hi,

    Can someone help please,

    I’m look to run a rule or macro within outlook that when an email arraives it searches the body for an email address then forwards it onto that address?

  6. Neil McGill
    March 31st, 2010 at 15:16 | #7

    @Dean
    Slightly modified the above code to run as a rule from Outlook.
    Simply set a rule which runs the script on incoming mails.
    NOTE: This is client-only and so runs only if Outlook is open.

    I use this to simulate test mails from external systems back into my own users domains.

    Sub FwdMailText(MyMail As MailItem)
    Dim objItem As Object
    Dim Msg As Outlook.MailItem
    Dim NewForward As Outlook.MailItem
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim emailtosend As String ‘no need to make this global
    On Error Resume Next

    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace(“MAPI”)
    ‘get the new mail ID
    Set Msg = olNS.GetItemFromID(MyMail.EntryID)
    Msg.BodyFormat = olFormatPlain
    ‘CALL THE SUB TO PARSE BODY TEXT AND GET THE EMAIL
    emailtosend = SearchBody(Msg.Body)
    ‘only continue if the mail address is returned
    If emailtosend “” Then
    Set NewForward = Msg.Forward
    With NewForward
    .Subject = Msg.Subject
    .To = emailtosend
    .Body = Msg.Body
    .Send
    End With
    With Msg
    .UnRead = False
    .FlagStatus = olNoFlag
    End With
    Else
    GoTo ExitProc
    End If
    ExitProc:
    Set NewForward = Nothing
    Set Msg = Nothing
    Set objItem = Nothing
    End Sub

    Function SearchBody(msgbody) As String
    lines = Split(msgbody, vbCrLf)
    ‘–GET THE EMAIL TO SEND TO
    For Each Line In lines
    If InStr(Line, “Test:”) Then
    parsed = Split(Line, “:”)
    If InStr(parsed(1), “@”) Then
    ‘return the email address
    SearchBody = parsed(1)
    End If
    End If
    Next
    End Function

  7. Neil McGill
    March 31st, 2010 at 15:20 | #8

    In the code above, the line
    If emailtosend “” Then
    should read If emailtosend > < “” Then

    I guess the site doesnt like angled brackets

  8. Neil McGill
    March 31st, 2010 at 15:21 | #9

    third times the charm – should be <>

  9. annabel hallam
    June 20th, 2010 at 19:16 | #10

    Hi I need a script that seraches the body of an email for an email address and then send a different message (not forwarding the original message) on – Is that possible and can you help?

    Thanks

  10. Aju
    December 21st, 2010 at 04:06 | #11

    I receive an e-mail which is a Daily Status Report from my team everyday which needs to be ‘Forwarded’ through my Outlook to the client. Keyword in subject line starts with “DSR -” . I need to add the client email ID in ‘To’ and our internal email alias in ‘cc’ . Since I am forwarding the message it should go without any change in formatting/style/fonts/table etc. Also i need to remove the forwarding text details from the body of the email at the start. Can anyone help me achieve this through a macro or rule in Outlook 2007.

    I want to trigger this email forwarding everyday as and when the Daily Status Report gets into my ‘Inbox’

  11. June 26th, 2011 at 01:10 | #12

    Hi,

    I have no outlook programming skills. How can I built a macro button which either:
    a) will send the selected email message as text file via ssh to a remote host?
    OR
    b) will send only the subject of selected email to a fixed email address?
    thank you.
    Rajeev

  12. rachael
    July 17th, 2011 at 13:45 | #13

    Thanks for posting your code! Can you tell me how to modify it to work for me? I receive tons of emails everyday that after I process I have to Forward them to the username listed in the body of the email. I would like to be able to run a macro that pulls the username from the body and puts it into the To field and adds the @domain (which is the same for every single username).

    Example body text:

    Location : Nevada
    Qty : 1
    Amt : 128.00
    Vendor: me
    User : rachml

    I would like to speed up the fwd’g process by pushing a button that returns rachml @domain.com (without a space) in the To: field of my message

    This would be after I have already hit Forward, so I can put a cutom note in the message, and this macro would be searching the text from the to-be-forwarded body.

    I am running Outlook 2003. I have no real knowledge of Macros or VB besides recording a macro and putting the button on the menu bar

  1. No trackbacks yet.

VAMOVE *

*