none
Need outlook VBScript to alter subject line, but retains sender information and forwards email RRS feed

  • Question

  • Hi all,

    I need a VBScript in outlook that alters the subject line, but retains and forwards the original sender's details

    Example,

    To: finance@company.com

    Sender: Joe Citizen Subject: Invoice123

    All emails needs to be redirected to a Jane@company.com with alteration to the subject line, but Joe Citizen's information still needs to be the one it's coming from (so Jane can reply back)

    To: Jane@company.com

    Sender: Joe Citizen Subject: ATTENTION Invoice 123

    Jane does not see that the email is being redirected from finance@company.com with the subject line altered. When Jane hit's reply, it will send to Joe Citizen.

    Would really appreciate any assistance with this.


    • Moved by Steve Fan Monday, July 18, 2016 7:56 AM VBScript
    Friday, July 15, 2016 7:12 AM

Answers

  • >>>But when it forwards, it's not retaining the original senders' details. I need it to re-direct rather than forward (so Jane can reply back to the original sender, and not to the forwarding address)

    Base on my testing, it does not change original sender when you forward MailItem, you could use below code to check:
    Sub DemoCheckSender()
    
      Dim myItem As Outlook.MailItem
      Dim objInsp As Outlook.Inspector
      Dim objActionsMenu As Office.CommandBarControl
      Dim olResendMsg As Outlook.MailItem
     
      ' get current item & open if needed
      On Error Resume Next
     
      Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
        
          Set myItem = Application.ActiveExplorer.Selection.Item(1)
          Debug.Print GetSMTPAddress(myItem)
          
          Set myForward = myItem.Forward
          myForward.Recipients.Add "xxxxxx@xxxx.com"
          myForward.Send
    
          Debug.Print GetSMTPAddress(myItem)
      End Select
      
    End Sub
    
    Function GetSMTPAddress(olkMsg As Outlook.MailItem) As String
        Dim olkSnd As Outlook.AddressEntry, olkExu As Outlook.exchangeUser
        Set olkSnd = olkMsg.sender
        If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
            Set olkExu = olkSnd.GetExchangeUser
            GetSMTPAddress = olkExu.PrimarySmtpAddress
        Else
            GetSMTPAddress = olkMsg.SenderEmailAddress
        End If
        Set olkSnd = Nothing
        Set olkExu = Nothing
    End Function

    Thursday, July 21, 2016 8:04 AM

All replies

  • Hi,

    This forum is for general questions and feedback related to Microsoft Outlook. Since your question is more related to VBScript request, I'll move it to the Outlook for Developers forum:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Steve Fan
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    Monday, July 18, 2016 7:55 AM
  • Hi jishri86,

    Acccording to your description, please correct me if I have any misunderstandings on your question. You could refer to below code:
    Sub ChangeSubjectForward(Item As Outlook.MailItem)
    
        Item.Subject = "ATTENTION Invoice 123"
        Item.Save
      
        Set myForward = Item.Forward
        myForward.Recipients.Add "Jane@company.com"
        myForward.Send
        
    End Sub
    For more information, click here to refer about MailItem Object (Outlook)

    Tuesday, July 19, 2016 2:42 AM
  • Hi David,

    Thanks for your response. It's half right where the subject line is changed.

    But when it forwards, it's not retaining the original senders' details. I need it to re-direct rather than forward (so Jane can reply back to the original sender, and not to the forwarding address)

    Wednesday, July 20, 2016 8:04 AM
  • >>>But when it forwards, it's not retaining the original senders' details. I need it to re-direct rather than forward (so Jane can reply back to the original sender, and not to the forwarding address)

    Base on my testing, it does not change original sender when you forward MailItem, you could use below code to check:
    Sub DemoCheckSender()
    
      Dim myItem As Outlook.MailItem
      Dim objInsp As Outlook.Inspector
      Dim objActionsMenu As Office.CommandBarControl
      Dim olResendMsg As Outlook.MailItem
     
      ' get current item & open if needed
      On Error Resume Next
     
      Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
        
          Set myItem = Application.ActiveExplorer.Selection.Item(1)
          Debug.Print GetSMTPAddress(myItem)
          
          Set myForward = myItem.Forward
          myForward.Recipients.Add "xxxxxx@xxxx.com"
          myForward.Send
    
          Debug.Print GetSMTPAddress(myItem)
      End Select
      
    End Sub
    
    Function GetSMTPAddress(olkMsg As Outlook.MailItem) As String
        Dim olkSnd As Outlook.AddressEntry, olkExu As Outlook.exchangeUser
        Set olkSnd = olkMsg.sender
        If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
            Set olkExu = olkSnd.GetExchangeUser
            GetSMTPAddress = olkExu.PrimarySmtpAddress
        Else
            GetSMTPAddress = olkMsg.SenderEmailAddress
        End If
        Set olkSnd = Nothing
        Set olkExu = Nothing
    End Function

    Thursday, July 21, 2016 8:04 AM