Share via

Outlook VBA - Save original message in the same folder as the reply

Anonymous
2023-03-16T14:54:22+00:00

Before having to swicth to Outlook (for my work) I used another email client. When I clicked to sent a message I was prompted where I wanted to save that message. If the message I was sending was a reply to a message the prompt (window) had a checkbox to save the original message in the same folder as well.

I have added this VBA code to Outlook (I copied it from the internet, I did not write it myself):


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objNS As NameSpace
Dim objFolder As Folder
Dim intFolder As Integer

intFolder = MsgBox("Do you want to save the message?", vbYesNoCancel + vbQuestion, "Save Message")
If intFolder = vbCancel Then
Cancel = True
Exit Sub
ElseIf intFolder = vbYes Then
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.PickFolder
If objFolder Is Nothing Then
Cancel = True
Exit Sub
End If
Set Item.SaveSentMessageFolder = objFolder
Else
Set Item.SaveSentMessageFolder = Nothing
End If
End Sub


This code brings up a prompt (message box) when I sent a message asking me if I want to save the message; if I say yes I can choose the folder where to save the message to.

Is it possible to add code to have a checkbox when the message is a reply to save the original message together with the reply to the same folder?

Outlook | Windows | Classic Outlook for Windows | For business

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2023-03-16T15:48:01+00:00

    Dear Herwin,

    Thank you also for your kind cooperation.

    I see. In that case, you can use the PickFolder method (source: https://stackoverflow.com/questions/12688476/di...*) to display a dialog box that allows you to select an Outlook folder for your reply and original message. You can use this method in your Application.ItemSend event handler and assign the selected folder to item.SaveSentMessageFolder for your reply. For your original message, you can use item.Reply.Parent.Move method to move it to the same folder as your reply. You can find an example of this approach here: https://forums.slipstick.com/threads/97981-macr...*.

    Alternatively, you can use a macro that runs after sending a message and prompts you to choose a folder for both the reply and the original message. You can find an example of this approach here: https://www.slipstick.com/outlook/email/choosin...*.

    More about Macros:

    A macro is a subroutine that performs a specific task or a series of tasks. You can write a macro by using Visual Basic for Applications (VBA), which is a programming language that runs inside Outlook and other Office applications.

    To write a macro for Outlook VBA, you need to follow these steps:

    (https://support.microsoft.com/en-gb/office/crea...)

    1. On the Developer tab of the Microsoft Office Fluent ribbon, click Visual Basic. If you don’t see the Developer tab, you can enable it by going to File > Options > Customize Ribbon and checking the box for Developer under Main Tabs.
    2. In the Project window, double-click the module you want to contain the macro. A module is a container for your code. If you don’t have any modules, you can add one by right-clicking on your project name and selecting Insert > Module.
    3. On the Insert menu, click Procedure. This will open a dialog box where you can name your macro and specify its type (Sub or Function) and scope (Public or Private).
    4. In the Name box, type a name for your macro. The name cannot contain spaces or special characters.
    5. Click OK. This will create an empty subroutine with your macro name in the code window.
    6. Type the code you want to run in the body of the subroutine between Sub and End Sub. You can use various objects, methods, properties and events from Outlook VBA to automate tasks such as sending emails, creating appointments, moving messages etc.
    7. Save your project by clicking on File > Save Project As… and choosing a location and name for your project file (.otm).
    8. Run your macro by clicking on Run > Run Sub/UserForm, pressing F5 key or using keyboard shortcuts such as Alt+F8.

    You can find more on that here: https://learn.microsoft.com/en-us/office/vba/ou...

    * Note: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.

    Best Regards,

    Georgw A.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-03-17T16:56:58+00:00

    Dear Herwin,

    I’m sorry to hear that. Outlook VBA coding can be challenging to learn and understand. Maybe you can find some online tutorials or courses that can help you improve your skills and knowledge.

    Alternatively, you can also try to use the built-in Outlook rules to manage your messages and folders. You can find more information about how to create and edit rules here: https://support.microsoft.com/en-us/office/mana...

    If it seems easier, you can modify your VBA code to use the FolderPicker method of the Application object to display a dialog box that allows you to select a folder where you want to save the reply and the original message. You can then assign that folder to the SaveSentMessageFolder property of both items.

    Here is an example of how you can do this:

    (source: https://stackoverflow.com/questions/26392482/vb...*)

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Dim objNS As NameSpace
        Dim objFolder As Folder
        Dim intFolder As Integer
        Dim objReply As MailItem
        
        intFolder = MsgBox("Do you want to save the message?", vbYesNoCancel + vbQuestion, "Save Message")
        
        If intFolder = vbCancel Then
            Cancel = True
            Exit Sub
        ElseIf intFolder = vbYes Then
            Set objNS = Application.GetNamespace("MAPI")
            Set objFolder = objNS.Session.PickFolder 'Use FolderPicker method here
            
            If objFolder Is Nothing Then
                Cancel = True
                Exit Sub
            End If
            
            Set Item.SaveSentMessageFolder = objFolder 'Set SaveSentMessageFolder property for reply item
            
            If TypeOf Item Is MailItem And Item.ReplyRecipients.Count > 0 Then 'Check if item is a reply
                
                Set objReply = Item.Reply(False) 'Get original message
                
                Set objReply.SaveSentMessageFolder = objFolder 'Set SaveSentMessageFolder property for original message
                
                objReply.Close olDiscard 'Close original message without saving changes
                
            End If
            
        Else
        
            Set Item.SaveSentMessageFolder = Nothing
        
        End If
    
    End Sub
    

    Please note that this code is not tested and may need some adjustments.

    * Note: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.

    Best Regards,

    George A.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-03-17T15:41:48+00:00

    Thanks George.

    I appreciate your response and efforts to help me find the answer.

    I am afraid the links are above my (limited) level of understanding of Outlook VBA coding.

    Best,

    Herwin

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-03-16T15:35:37+00:00

    Thanks George.

    Much appreciated.

    Actually I need the prompt to choose a folder where to save the reply (and the original message), as in 99.99% of the case the original message will be in the inbox, and I need to save the reply and original message to another folder.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-03-16T15:27:30+00:00

    Dear Herwin_H,

    I’m happy to help you with your Outlook VBA question.

    You can actually use the Reply event of the MailItem object to set the SaveSentMessageFolder property for the reply item to the folder in which the original item resides. This way, you can save both the original message and the reply in the same folder without having to pick a folder manually.

    Here is an example of how you can use this event in your VBA code:

    Private WithEvents myItem As Outlook.MailItem
    
    Sub Initialize_Handler()
     Set myItem = Application.ActiveInspector.CurrentItem
    End Sub
    
    Private Sub myItem_Reply(ByVal Response As Object, Cancel As Boolean)
     Set Response.SaveSentMessageFolder = myItem.Parent
    End Sub
    

    More on that here: https://learn.microsoft.com/en-us/office/vba/ap....

    I hope this helps you with your task.

    Best Regards,

    George A.

    Was this answer helpful?

    0 comments No comments