none
Macro to Forward and Move Email? RRS feed

  • Question

  • I want to add a custom button to Outlook 2013 that when I press it forwards the email to a pre-defined address and changes the sender address (from) to a standard address (as I'm using the button on multiple accounts but it needs to come from the same account), then, moves the message to a predefined folder.

    I'd prefer the forwarded message not get saved to my sent items folder (I'm using IMAP)

    Also, I'm viewing my e-mail from a macro that created a unified inbox from multiple accounts, but when I forward from the unified view, it need to move into a specific folder in one account.  I've noted though that the built-in move to OneNote function doesn't work with this unified view, only from the inbox of the various accounts.  I can live with having to go into each account and press the button to forward and move, but I'd prefer not to.

    Looking for code examples. Thanks in advance for the help!

    Monday, January 13, 2014 7:36 PM

Answers

  • Hello Consultant1027,

    Unfortunately there is no any ready made macro exactly for your business logic. You need to develop a solution on your own.

    The Customizing the 2007 Office Fluent Ribbon for Developers articles describes the required steps for customizing the Outlook UI in depth. Also you may find the Customizing the Office 2010 Backstage View for Developers article helpful.

    The MailItem class which represents an Outlook e-mail message provides the following properties you may be interested in:

    • The Send method has the following description in MSDN: The Send method sends an item using the default account specified for the session. In a session where multiple Microsoft Exchange accounts are defined in the profile, the first Exchange account added to the profile is the primary Exchange account, and is also the default account for the session. To specify a different account to send an item, set the SendUsingAccount property to the desired Account object and then call the Send method.
    • The SendUsingAccount property: Returns or sets an Account object that represents the account under which the MailItem is to be sent.

    For example:

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
     If oAccount.AccountType = olPop3 Then  
     Dim oMail As Outlook.MailItem  
     Set oMail = Application.CreateItem(olMailItem)  
     oMail.Subject = "Sent using POP3 Account"  
     oMail.Recipients.Add ("someone@example.com")  
     oMail.Recipients.ResolveAll  
     oMail.SendUsingAccount = oAccount  
     oMail.Send  
     End If  
     Next  
    End Sub 
    

    • The SaveSentMessageFolder method: Returns or sets a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.
    • The Move method: it moves a Microsoft Outlook item to a new folder.

    For example:

    Sub MoveItems() 
     Dim myNameSpace As Outlook.NameSpace 
     Dim myInbox As Outlook.Folder 
     Dim myDestFolder As Outlook.Folder 
     Dim myItems As Outlook.Items 
     Dim myItem As Object 
     
     Set myNameSpace = Application.GetNamespace("MAPI") 
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
     Set myItems = myInbox.Items 
     Set myDestFolder = myInbox.Folders("Personal Mail") 
     Set myItem = myItems.Find("[SenderName] = 'Dan Wilson'") 
     While TypeName(myItem) <> "Nothing" 
     myItem.Move myDestFolder 
     Set myItem = myItems.FindNext 
     Wend 
    End Sub
    
    Tuesday, January 14, 2014 1:45 PM

All replies

  • Hello Consultant1027,

    Unfortunately there is no any ready made macro exactly for your business logic. You need to develop a solution on your own.

    The Customizing the 2007 Office Fluent Ribbon for Developers articles describes the required steps for customizing the Outlook UI in depth. Also you may find the Customizing the Office 2010 Backstage View for Developers article helpful.

    The MailItem class which represents an Outlook e-mail message provides the following properties you may be interested in:

    • The Send method has the following description in MSDN: The Send method sends an item using the default account specified for the session. In a session where multiple Microsoft Exchange accounts are defined in the profile, the first Exchange account added to the profile is the primary Exchange account, and is also the default account for the session. To specify a different account to send an item, set the SendUsingAccount property to the desired Account object and then call the Send method.
    • The SendUsingAccount property: Returns or sets an Account object that represents the account under which the MailItem is to be sent.

    For example:

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
     If oAccount.AccountType = olPop3 Then  
     Dim oMail As Outlook.MailItem  
     Set oMail = Application.CreateItem(olMailItem)  
     oMail.Subject = "Sent using POP3 Account"  
     oMail.Recipients.Add ("someone@example.com")  
     oMail.Recipients.ResolveAll  
     oMail.SendUsingAccount = oAccount  
     oMail.Send  
     End If  
     Next  
    End Sub 
    

    • The SaveSentMessageFolder method: Returns or sets a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.
    • The Move method: it moves a Microsoft Outlook item to a new folder.

    For example:

    Sub MoveItems() 
     Dim myNameSpace As Outlook.NameSpace 
     Dim myInbox As Outlook.Folder 
     Dim myDestFolder As Outlook.Folder 
     Dim myItems As Outlook.Items 
     Dim myItem As Object 
     
     Set myNameSpace = Application.GetNamespace("MAPI") 
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
     Set myItems = myInbox.Items 
     Set myDestFolder = myInbox.Folders("Personal Mail") 
     Set myItem = myItems.Find("[SenderName] = 'Dan Wilson'") 
     While TypeName(myItem) <> "Nothing" 
     myItem.Move myDestFolder 
     Set myItem = myItems.FindNext 
     Wend 
    End Sub
    
    Tuesday, January 14, 2014 1:45 PM
  • Hi,

    >> then, moves the message to a predefined folder.<<
    Did you mean you want to move the message to the specific folder in the current account? Did the suggestion provided by Eugene fix the issue?

    If you have any question, please feel free to let us know.

    Best regards
    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 16, 2014 11:07 AM
    Moderator