Avatar of RWayneH
RWayneHFlag for United States of America

asked on 

VBA to Forward an email from Inbox.

Is it possible to preform the following steps on an Outlook email in VBA?  After filtering the InBox, I need to perform this on a bunch of emails.

1. Copy current existing email Subject:
2. Click Forward
3. Delete the top portion, in the body of the email, up to and including the line that has Subject: (this would be the old Subject, prior to Forward click in Step 1)
4. Remove FW: from the new Subject field, (or use Subject from Step 1)
5. Find the first email address in the body of the email and replace what is in the To: field with that.
6. In the body of the email there is the following:  Partner Number: 23611  I need to harvest the number after it.  23611  (some or 5 digits others are 6 could be a 7)

Looking for some guidance, on writing this Sub in OutLook VBA.

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of RWayneH
Flag of United States of America image


Hello and thanks.  I wrote most of the process and it is working good so far.  The only thing that I am not getting is how to get the email address for Instr?  The value of RRelVal2 is always finding it, but I am having issue getting the actual value to use for myforward.To  "[email]".
Any ideas?  Please advise and thanks.

Sub ForwardEmails()
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Dim MsgTxt As String
    Dim x As Integer
    Dim myNms As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim myRecipient As Outlook.Recipient
    Dim myItem As MailItem
    Dim myforward
    Dim TheOldSubject
    Dim EmailAdd
    Dim strFind
    Dim RRelVal2 As String
    Set myOlExp = myOlApp.ActiveExplorer
    Set myOlSel = myOlExp.Selection
    Set myItem = myOlSel.Item(1)
    TheOldSubject = myItem.Subject 'for after message forward
    'For x = 1 To myOlSel.Count  'plan is to filter InBox so only the items that need to be forwarded are visible.
        Set myforward = myItem.Forward
        myforward.Body = Replace(myforward.Body, "-----Original Message-----", "")
        myforward.Body = Replace(myforward.Body, "From: batchsd <batchsd@Haworth.com>", "")
        myforward.Body = Replace(myforward.Body, "To: SAPDocuments <SAPDocuments@Haworth.com>", "")
        myforward.Body = Replace(myforward.Body, "Subject:", "")
'        myforward.Body = Replace(myforward.Body, "Sent:", "") 'after "Sent:" is the Date... need a way to delete the rest? WildCard?
        myforward.Body = ReplacedBody(myforward.Body)
        strFind = "@" 'strFind is what to look for in variable StatusBarMess
        RRelVal2 = InStr(1, myItem.Body, strFind)
        If RRelVal2 > 1 Then
            'an email was found.
        End If

       ' EmailAdd = Trim(Mid(myItem.Body, InStr(myItem.Body, "@") + 7, 1))
        'data is in myItem>Body  Instr on "@"??
        myforward.To = "dummyName@dummyServer.com"   'Need to add variable name harvested email from .body
        myforward.Subject = TheOldSubject
    'Next x
End Sub

Open in new window

<<The value of RRelVal2 is always finding it, but I am having issue getting the actual value to use for myforward.To  "[email]"..>>

 Not quite sure I follow what you mean....once you find the position of the @, then you would use functions like Left(), Mid(), etc to parse the address out of the string.   I would be looking for "To:" rather than the @ and then work from there.

 But there's more to handling e-mail addresses than what's in the body of the message on a forward.

 Because of that, my suggestion would not be to try and forward and modify the message, but rather create a new e-mail message and copy the text from the e-mail you found.   You might find that simpler in the long run.

 However it's not entirely clear to me what it is your trying to achieve.

Avatar of RWayneH
Flag of United States of America image


Thanks the reference links helped a lot.  Appreciate the help.
No problem.   Did you stick with the forward, or create a new message and copy the body?

Avatar of RWayneH
Flag of United States of America image


I had to use Forward... it was a very odd scenario that we had to resend some emails that went to the wrg recipient, so after using forward I had to remove the header, reset the Subject and replace the recipient based on data collected from the body of the email.  Over 1500+ emails.  It worked pretty well, just got a bit tricky doing all the Instr's and the Chr(13)'s to collect the data properly.
Thanks.   Was just curious.


Microsoft Outlook is a personal information manager from Microsoft, available as a part of the Microsoft Office suite. Although often used mainly as an email application, it also includes a calendar, task manager, contact manager, note-taker, journal, and web browser.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo