1

I want after replying to the active mail to delete that active mail then open the next item in the folder.

I found this answer, added 'olItem.Delete' but need the 'open next mail item'.

Sub ReplyMSG()
    Dim olItem As Outlook.MailItem
    Dim olReply As MailItem ' Reply
    Dim olRecip As Recipient ' Add Recipient
    Dim objInsp As Outlook.Inspector
    Dim objActionsMenu As Office.CommandBarControl

    For Each olItem In Application.ActiveExplorer.Selection
        Set olReply = olItem.Reply
        olReply.HTMLBody = "Thank you!" & vbCrLf & olReply.HTMLBody
        olReply.Send objInsp.CommandBars.ExecuteMso(478)
        objActionsMenu.Execute
    Next olItem
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joe Patrick
  • 51
  • 1
  • 11
  • "Next mail item" in what sense? Next in the selection? In the folder? – Dmitry Streblechenko Feb 09 '16 at 19:42
  • In the folder, thanks! – Joe Patrick Feb 09 '16 at 23:03
  • That Question is closed, what is your trying to do? also see [SO Help](http://stackoverflow.com/tour) – 0m3r Feb 10 '16 at 06:34
  • Hi, I'm looking for code that will (1) reply to the active email (2) add text (3) send (4) delete it (move to deleted folder) then (5) open the next email item in the folder. I need step number 5, please. The answer I referenced in my original post provides steps 1 through 3 and I figured out step 4 from another answer. Now I just need step 5. Thanks! – Joe Patrick Feb 10 '16 at 12:23
  • Thanks, @Om3r & @niton! Om3r, that does not open the next item. Niton, I'm pickin' up what you're layin' down but not sure if I've found the right ID. I don't see an "icon" where I customize the toolbar/ribbon and everything I hover over just gives me the shortcut. I found 478 through the link/download but not sure how to incorporate it into my code. – Joe Patrick Feb 12 '16 at 18:17

2 Answers2

1

You should process Activeinspector.CurrentItem not the selection. The code in that link will behave differently than expected if you, say leave mail open, make other selections in the explorer window, come back to the original mail which is now not selected.

To go to the next item after deleting, try simulating a click on the delete button.

Both these ideas are demonstrated in this answer.

Sub SetEditMode()
Dim myItem As Outlook.MailItem
Dim objInsp As Outlook.Inspector
Dim objActionsMenu As Office.CommandBarControl
Dim olNewMailItem As Outlook.MailItem

On Error Resume Next ' Bad coding do not do this

Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set myItem = ActiveExplorer.Selection.Item(1)
        myItem.Display
    Case "Inspector"
        Set myItem = ActiveInspector.CurrentItem
    Case Else
End Select
If myItem Is Nothing Then GoTo ExitProc
'edit mode
Set objInsp = ActiveInspector
objInsp.CommandBars.ExecuteMso ("EditMessage")
objActionsMenu.Execute

ExitProc:
End Sub

Use this idea to refer to your mail.

Set myItem = ActiveInspector.CurrentItem

Use this idea to click a button.

objInsp.CommandBars.ExecuteMso ("EditMessage")

EditMessage is an Mso ID / Control ID. You can find the Mso ID for the Delete button by hovering over the icon where you add buttons to the Quick Access toolbar or a ribbon.

You can download a list here 2007 Office System Document: Lists of Control IDs

Edit: 2016-02-22

Control ID may not work as I thought. Use the hover technique.

Sub Reply_ExecuteMso_DeleteOriginal()
'
' ** NOT working as intended **
'
' Simulating clicking the delete button is too fast
'  Must slow down with a MsgBox
'
' Now it is the same as clicking delete manually
'
Dim olItem As Object
Dim olReply As mailItem
Dim objInsp As Inspector

Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set olItem = ActiveExplorer.Selection.Item(1)
        olItem.Display
    Case "Inspector"
        Set olItem = ActiveInspector.currentItem
End Select

If olItem Is Nothing Then GoTo ExitProc

If olItem.Class = olMail Then

    Set olReply = olItem.Reply
    olReply.Display

    olReply.HTMLBody = "Thank you!" & vbCrLf & olReply.HTMLBody
    olReply.send

    olItem.Display
    Set objInsp = ActiveInspector
    objInsp.CommandBars.ExecuteMso ("Delete")

End If

MsgBox "This slows down the processing."

On Error Resume Next
' Error when no items left
Set olItem = ActiveExplorer.Selection.Item(1)
olItem.Display
On Error GoTo 0

ExitProc:
    Set olItem = Nothing
    Set olReply = Nothing
    Set objInsp = Nothing

End Sub
Community
  • 1
  • 1
niton
  • 8,771
  • 21
  • 32
  • 52
0

Remember to update Folder Name

olFolderName = "TEMP"

Sub Open_Next_olMessage()
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder As MAPIFolder
    Dim olItem As MailItem
    Dim olReply As MailItem
    Dim olFolderName As String
    Dim olMsgBox As Integer
    Dim Cancel As Boolean

    olFolderName = "TEMP" '<-- Update Folder Name

'   // Set Inbox/SubFolder
    Set olNamespace = Session.Application.GetNamespace("MAPI")
'                                                             --> Folder Name
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders(olFolderName)

'   // Loop through items in folder
    For Each olItem In olFolder.Items
        If (olItem.Class = olMail) Then
            olItem.Display True

            olMsgBox = MsgBox("Do you want to Reply to this Email", vbYesNoCancel)
            Set olReply = olItem.ReplyAll ' or Reply
            Set olItem = olItem

            If olMsgBox = vbCancel Then
                Cancel = True ' Exit
                Exit Sub
            ElseIf olMsgBox = vbYes Then
                   olReply.Display True
            ElseIf olMsgBox = vbNo Then
'                  do something
            End If

            olItem.Delete ' Delete Message
        End If
    Next

End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • @joepatrick are you selecting multiple items? Are getting any errors? also remove `.Send` from the code. – 0m3r Feb 14 '16 at 01:20
  • The current code works on multiple items (if selected) or just one item if it's open and therefore active and the only one selected. I need the .send for it to send. I'm still just lacking the part to make it automatically open the next item in the folder succeeding the item just deleted. – Joe Patrick Feb 27 '16 at 17:35
  • Still confused- why do you want it open- for editing? – 0m3r Feb 27 '16 at 17:57
  • So i can read it. I'd like it to open automatically. Like when you set your email options for "after moving or deleting an open item:" to "open the next item". – Joe Patrick Feb 29 '16 at 23:17