Macro to move reply and original message to folder

Status
Not open for further replies.

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
 
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.
 
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.
 
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
 
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.
 
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.
 
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
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
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
J Outlook macro to run before email is being send Outlook VBA and Custom Forms 0
H Macro to Delete Duplicate items in Outlook calendar where title is the same and date is the same Outlook VBA and Custom Forms 0
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 1
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 0
S macro error 4605 Outlook VBA and Custom Forms 0
A Macro Mail Alert Using Outlook 4
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to check Cursor & Focus position Outlook VBA and Custom Forms 8
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
M Outlook 2019 Macro not working Outlook VBA and Custom Forms 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
V Macro to count flagged messages? Using Outlook 2
sophievldn Looking for a macro that moves completed items from subfolders to other subfolder Outlook VBA and Custom Forms 7
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Macro to block senders domain Outlook VBA and Custom Forms 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
J Macro to Insert a Calendar Outlook VBA and Custom Forms 8
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6

Similar threads

Back
Top