Macro to move reply and original message to folder

tanko

New Member
Outlook version
Outlook 365 64 bit
Email Account
IMAP
I use the code below in ThisOutlookSession to launch a folder picker for each message sent from Outlook. If the message is a reply/replyall, I then have to go to the message I was replying to and separately move that message to the same folder using Ctrl-Shift-V. I would love to be able to modify the code below such that it finds the original message and moves it to the same folder as I selected for the sent reply. Can anyone help?


Code:
Private Sub Application_ItemSend(ByVal Item As Object, _
    Cancel As Boolean)
  Dim objNS As NameSpace
  Dim objFolder As MAPIFolder
  Set objNS = Application.GetNamespace("MAPI")
  Set objFolder = objNS.PickFolder
  If TypeName(objFolder) <> "Nothing" And _
     IsInDefaultStore(objFolder) Then
      Set Item.SaveSentMessageFolder = objFolder
  End If
  Set objFolder = Nothing
  Set objNS = Nothing
End Sub

Public Function IsInDefaultStore(objOL As Object) As Boolean
  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim objInbox As Outlook.MAPIFolder
  Dim objConvoID As String
 
  On Error Resume Next
  Set objApp = CreateObject("Outlook.Application")
  Set objNS = objApp.GetNamespace("MAPI")
  Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
  Select Case objOL.Class
    Case olFolder
      If objOL.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case olAppointment, olContact, olDistributionList, _
         olJournal, olMail, olNote, olPost, olTask
      If objOL.Parent.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case Else
      MsgBox "This function isn't designed to work " & _
             "with " & TypeName(objOL) & _
             " items and will return False.", _
             , "IsInDefaultStore"
  End Select
 
  Set objApp = Nothing
  Set objNS = Nothing
  Set objInbox = Nothing
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'm on my MacBook right now, so I can't test anything.... but a search for the subject and/or first part of a conversation id would work.

The macro in this article could be a basis for it - it searches on the subject and to/from. The to/from helps avoid finding messages with the same subject from different people - as long as this sender doesn't always use the same subject (like one of my clients does.) The scope on it is for all mailboxes - use current folder scope so it doesn't find messages you already moved.
Find messages in a conversation

The other option is to search for the conversation id - it should be on the reply. I forget offhand how many characters are in the original conversation id - but that is what you'd use.
 

tanko

New Member
Outlook version
Outlook 365 64 bit
Email Account
IMAP
Thanks, Diane. I had tried to include a Find function for messages with the same conversation ID, but it wasn't working. I think the problem is actually that I fundamentally don't understand event-based Outlook macros. I've gotten pretty good at VBA for Word, but Outlook seems like kind of a different world.
 

tanko

New Member
Outlook version
Outlook 365 64 bit
Email Account
IMAP
OK, I tried a little tinkering and almost have it. I want to move just the parent of the message being sent, not necessarily all conversation items in the inbox, so I've been playing with the "GetParent." I seem to be almost there, except that I can't seem to figure out how to tell VBA to get the parent of the message I am composing and then sending.

The following code errors out at

Code:
Set oParent = oConvo.GetParent(oMail)
. I think this is because the message in the composer window is a draft and not a completed message, so it is not finding its conversation ID.

Can you help?

Code:
Private Sub Application_ItemSend(ByVal Item As Object, _
    Cancel As Boolean)
  Dim objNS As NameSpace
  Dim objFolder As MAPIFolder
  Dim oMail As MailItem
  Dim oConvo As Outlook.Conversation
  Dim myOlApp As New Outlook.Application
  Dim oParent As MailItem
 
  Set objNS = Application.GetNamespace("MAPI")
  Set objFolder = objNS.PickFolder
  Set oMail = ActiveInspector.CurrentItem
  Set oConvo = oMail.GetConversation

  Set oParent = oConvo.GetParent(oMail)
  Debug.Print oParent.SentOn
 
  If TypeName(objFolder) <> "Nothing" And _
     IsInDefaultStore(objFolder) Then
      Set Item.SaveSentMessageFolder = objFolder
      oParent.Move objFolder
  End If
  Set objFolder = Nothing
  Set objNS = Nothing
  Set myOlApp = Nothing
  Set oMail = Nothing
  Set oConvo = Nothing
  Set oParent = Nothing

End Sub

Public Function IsInDefaultStore(objOL As Object) As Boolean
  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim objInbox As Outlook.MAPIFolder
 
  On Error Resume Next
  Set objApp = CreateObject("Outlook.Application")
  Set objNS = objApp.GetNamespace("MAPI")
  Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
  Select Case objOL.Class
    Case olFolder
      If objOL.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case olAppointment, olContact, olDistributionList, _
         olJournal, olMail, olNote, olPost, olTask
      If objOL.Parent.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case Else
      MsgBox "This function isn't designed to work " & _
             "with " & TypeName(objOL) & _
             " items and will return False.", _
             , "IsInDefaultStore"
  End Select
 
  Set objApp = Nothing
  Set objNS = Nothing
  Set objInbox = Nothing
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
because the message in the composer window is a draft and not a completed message
Yeah. I think what you need is an reply macro that picks up the parent at the time you hit reply. Maybe either get the parent or move it and set the move to folder value on the reply.
 

tanko

New Member
Outlook version
Outlook 365 64 bit
Email Account
IMAP
Thanks! I've been working on this but I can't figure out how to get an event listener inside of a sub. Basically, my code would look something like this, but
Code:
 Sub Application_ItemSend
does not work inside of
Code:
Sub afterReply
. Essentially this code should define the parent as oItem for safekeeping and then move it when it has gotten the objFolder destination.

Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
  
Private Sub Application_Startup()
   Set oExpl = Application.ActiveExplorer
   bDiscardEvents = False
End Sub
  
Private Static Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
   Dim oRand As MailItem
   Dim oParent As MailItem
   Set oParent = oItem

End Sub
  
' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
    
Set oResponse = oItem.Reply
 afterReply
End Sub

Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Forward

 afterReply
End Sub

Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.ReplyAll

 afterReply
End Sub

Private Sub afterReply()
    oResponse.Display
'------------------------------------------------PROBLEM HERE-----------------------------------------
    Application_ItemSend(ByVal oResponse As Object, _
       Cancel As Boolean)
'------------------------------------------------PROBLEM HERE-----------------------------------------    
 End Sub


Private Sub Application_ItemSend(ByVal Item As Object, _
    Cancel As Boolean)
  Dim objNS As NameSpace
  Dim objFolder As MAPIFolder
  Dim itemParentFolder As MAPIFolder
  Dim oParent As MailItem
  Set objNS = Application.GetNamespace("MAPI")
  Set objFolder = objNS.PickFolder
  If TypeName(objFolder) <> "Nothing" And _
     IsInDefaultStore(objFolder) Then
      Set Item.SaveSentMessageFolder = objFolder
      oParent.Move (objFolder)
  End If
  Set objFolder = Nothing
  Set objNS = Nothing
  Set oParent = Nothing
End Sub

Public Function IsInDefaultStore(objOL As Object) As Boolean
  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim objInbox As Outlook.MAPIFolder
  On Error Resume Next
  Set objApp = CreateObject("Outlook.Application")
  Set objNS = objApp.GetNamespace("MAPI")
  Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
  Select Case objOL.Class
    Case olFolder
      If objOL.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case olAppointment, olContact, olDistributionList, _
         olJournal, olMail, olNote, olPost, olTask
      If objOL.Parent.StoreID = objInbox.StoreID Then
        IsInDefaultStore = True
      End If
    Case Else
      MsgBox "This function isn't designed to work " & _
             "with " & TypeName(objOL) & _
             " items and will return False.", _
             , "IsInDefaultStore"
  End Select
  Set objApp = Nothing
  Set objNS = Nothing
  Set objInbox = Nothing
End Function
Yeah. I think what you need is an reply macro that picks up the parent at the time you hit reply. Maybe either get the parent or move it and set the move to folder value on the reply.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I would set a variable to equal the parent message then in the itemssend, check to see if the variable is used and move it. Or use error handler so it won't error if it's a new message.

Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean


Dim parentMail As Variant

'https://slipstick.me/44b0w
 
Private Sub Application_Startup()
   Set oExpl = Application.ActiveExplorer
   bDiscardEvents = False
End Sub
 
Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
End Sub




' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)

   Cancel = True
   bDiscardEvents = True
 
   Dim oResponse As MailItem
   Set oResponse = oItem.Reply
   Set parentMail = oItem
   oResponse.Display
    
   bDiscardEvents = False
Set oItem = Nothing
End Sub



Private Sub Application_ItemSend(ByVal Item As Object, _
    Cancel As Boolean)
  Dim objNS As NameSpace
  Dim objFolder As MAPIFolder
  Dim oMail As MailItem
  Dim oConvo As Outlook.Conversation
  Dim myOlApp As New Outlook.Application
  Dim oParent As MailItem
 
  Set objNS = Application.GetNamespace("MAPI")
  Set objFolder = objNS.PickFolder
  Set oMail = ActiveInspector.CurrentItem
  Set oConvo = oMail.GetConversation

  Set oParent = oConvo.GetParent(oMail)
  Debug.Print oParent.SentOn
 
  If TypeName(objFolder) <> "Nothing" And _
     IsInDefaultStore(objFolder) Then
      Set Item.SaveSentMessageFolder = objFolder
      oParent.Move objFolder


if not parentMail is nothing then
parentMail.move objFolder
end if

  End If
  Set objFolder = Nothing
  Set objNS = Nothing
  Set myOlApp = Nothing
  Set oMail = Nothing
  Set oConvo = Nothing
  Set oParent = Nothing

End Sub
 
Similar threads
Thread starter Title Forum Replies Date
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
B Macro to manually move selected emails to network folder Outlook VBA and Custom Forms 1
G email returns after running macro to move emails Outlook VBA and Custom Forms 1
Douglas Littlefield Macro to move E-mail Outlook VBA and Custom Forms 10
S Macro to print & move selected emails? Using Outlook 3
Douglas Littlefield Macro to move mail from inbox to Managed Folder Exchange Server Administration 1
L Macro Move E-mail attachments to a PC Folder Using Outlook 16
G Macro to move sent items from local folder to IMAP folder Using Outlook 4
S Outlook macro to move replied / forwarded emails to a seperate folder Using Outlook 1
C Help with a Macro to move emails to a different PST data file Using Outlook 4
J Macro to move mail from Inbox if older than 4 days Using Outlook 4
D Autosort macro for items in a view Outlook VBA and Custom Forms 2
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
S Visual indicator of a certain property or to show a macro toggle Outlook VBA and Custom Forms 2
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 8
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
witzker HowTo start a macro with an Button in OL contact form Outlook VBA and Custom Forms 12
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 4
U Macro for reminders,tasks,calendar Outlook VBA and Custom Forms 4
V macro runs slower on startup than after Outlook VBA and Custom Forms 3
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Using Outlook 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
O Run macro automatically at sending an email Using Outlook 11

Similar threads

Top