vbaemailoutlookoutlook-2013

Outlook VBA macro appears not to be executing


I would like to redirect a message received from a specific email to another email address. The email address to which the message will be redirected to will never be the same.

I thought of using scripts to accomplish this, where the email address to which the email must be redirected to subject of the email. The format of the subject of the original message is:

subject/redirectAddress@email.com

I have written the following script but when the rule executes the script nothing happens. Can you pleas tell me what mistake I made and why this script appears to do nothing?

Sub CodeSubjectForward(Item As Outlook.MailItem)
  Dim txt As String
  Dim EAddress As String
  Dim Temp As String
  Dim pos

  txt = Item.Subject
  pos = InStr(txt, "/")
  Temp = Left(txt, pos - 1)
  EAddress = Right(txt, Len(txt) - pos - 1)

  Dim objMsg As MailItem
  Set objMsg = Application.CreateItem(olMailItem)

  objMsg.body = Item.body
  objMsg.Subject = Temp
  objMsg.Recipients.Add EAddress

  objMsg.Display
End Sub

Solution

  • Open a mailitem that fits the rule conditions and step through this.

    Option Explicit
    
    Private Sub CodeSubjectForward_Test()
        Dim currItem As MailItem
        Set currItem = ActiveInspector.currentItem
        CodeSubjectForward currItem
    End Sub
    

    If you get to CodeSubjectForward then the rule conditions are not correct.