Outlook - Automatically forwarding e-mails when the server does not support it

I was recently allocated a Microsoft BPOS Online Services Exchange account for a client and I was configuring the same in Outlook. To keep up with the emails on the new account, I wanted to receive all incoming/outgoing emails for this BPOS account to my another email account that is synced across multiple devices I use.

Should be pretty easy I thought, and I set-up a Rule to automatically forward all incoming mail to my other account. But when I tested it, it did not work. Googling revealed that by default, BPOS Exchange Online account emails cannot be forwarded to another email outside the domain, but administrators can set up a rule for it via PowerShell.

What the heck I thought, there should be some way to get around this. My developr roots quickly took me to the Macros section in Outlook where I started fiddling with VBA code to monitor incoming emails.

After some tries (and googling), I was able to settle down with Outlook's NewMailEx event for the purpose and I set-up the following code to automatically forward all incoming emails to another account (via VBA code):

 

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim varEntryIDs
    Dim objItem
    Dim i As Integer
    varEntryIDs = Split(EntryIDCollection, ",")
    For i = 0 To UBound(varEntryIDs)
        Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
        'MsgBox (varEntryIDs(i))
                
        Set myItem = objItem.Forward
        myItem.Recipients.Add "other.account@example.com"
        myItem.Send
        
        'myItem.Delete

        myItem = Nothing
    Next
End Sub

And when I tested it, it worked perfectly. I thought there might be other people in my situation, so I shared the code here.

For completeness sake, the following 2 Outlook event handlers now forward all incoming email for my Exchange account to another email id, as well as add my other id automatically to BCC list when sending email from the Exchange account:

 

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
 Dim objRecip As Recipient
    Dim strMsg As String
    Dim res As Integer
    Dim strBcc As String
    On Error Resume Next

    ' #### USER OPTIONS ####
    ' address for Bcc -- must be SMTP address or resolvable
    ' to a name in the address book
    strBcc = "other.account@example.com"

    Set objRecip = Item.Recipients.Add(strBcc)
    objRecip.Type = olBCC
    If Not objRecip.Resolve Then
        strMsg = "Could not resolve the Bcc recipient. " & _
                 "Do you want still to send the message?"
        res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
                "Could Not Resolve Bcc Recipient")
        If res = vbNo Then
            Cancel = True
        End If
    End If

    Set objRecip = Nothing
End Sub


Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim varEntryIDs
    Dim objItem
    Dim i As Integer
    varEntryIDs = Split(EntryIDCollection, ",")
    For i = 0 To UBound(varEntryIDs)
        Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
        'MsgBox (varEntryIDs(i))
                
        Set myItem = objItem.Forward
        myItem.Recipients.Add "other.account@example.com"
        myItem.Send
        
        'myItem.Delete
        
        myItem = Nothing
    Next
End Sub

Outlook forwarding mails when server does not support itThe above code should be written in Outlook's "ThisOutlookSession" section under "Microsoft Office Outlook" as shown the the adjoining screenshot (click to enlarge it).

Please note that this will work for incoming/outgoing mails when accessed from the particular Outlook instance where the code has been configured. If you try to access the Exchange account from OWA or another email client (e.g. on your mobile device), this simply won't work as the code runs locally within Outlook (and not on your email server). Also if you delete/archive an incoming email from let's say OWA, the same won't be captured by the above code and hence won't be forwarded to your another account.

But its still better than nothing, the only sureshot way of forwarding all incoming email from BPOS online account is to request your domain administrator to create a rule specific for your mail account.

 

Tags: 

Comments

Hi,

How can I redirect email messages instead of forwarding using VB Code?

Thank you

rahul's picture

Hi Matt, I am not sure what exactly are you referring to as "redirect" here, but the email has already arrived in your mailbox. I think the best you can do is one of the following:

  1. Forward the email to your alternate mailbox.
  2. Forward the email to your redirection mailbox and then delete the original mail.
  3. Send an auto-reply to the sender of the email asking him/her to send the email to alternate mailbox.

Hello Rahul,

Your code is working but it leaves forwarded mails in the Sent Items folder.

Can we set this code to forward received mails and delete forwarded mails (not the original) permanently in the Sent Items folder?

Best Regards,
Sertaç

rahul's picture

Hi Sertac, I haven't researched into this. Please see if the Send method accepts a parameter to control whether to save the email or not. If such a paremeter is not available, you can delete the forwarded email from Sent Items after sending it.

Please note this code would not forward emails that were received when Outlook was closed.

Hi Rahul,

Looks like this is exactly what I need. Laughing

Thank you so much for sharing your solutions with us. 

br,

Laszlo

rahul's picture

Hi Laszlo, please note this code would not forward emails that were received when Outlook was closed.

dear sir your code is ok working fine but i need to delete the sent mail after forwarding from the sent items folder and from the deleted items folder

i tried myItem.DeleteAfterSubmit = True

but no luck

rahul's picture

Hi ahmad, I haven't tried this myself, but I am sure there must be an api method to do this (or maybe a flag which prevents the mail from being saved in Sent Items folder). If you aren't able to find any of these, maybe you can set a flag on the email (as a custom mapi property etc) and then setup a timer which deletes all emails from "Sent Items" folder that have this mapi property set.

I am unable to assist further owing to my schedule, I advise you try searching on the web, you should find something related.

Hi there, 

Thanks for the great plugin, does exactly what's its supposed to, but I keep getting an error

-----

Run-time error '91':

Object variable or With block variable not set.

-----

Am I missing something else that I need to do?

rahul's picture

Hi Mike, which line you are getting that error at.
BTW, I assembled this code a long time back for a colleague and I might not be able to look into fixing/enhancing it now.

rahul's picture

Hi Albert,

What's your exact query?

rahul's picture

Hi Madhukar,

What's your query?