Consulting

Results 1 to 13 of 13

Thread: Outlook VBA - move message then open

  1. #1

    Outlook VBA - move message then open

    I'm new to VBA in Excel and have never used any VBA in Outlook.
    I'm looking for VBA to move an email, then open the email.

    Thanks

  2. #2
    Move it from where to where? As you haven't said, the following will move the selected message to the folder you select then open the message

    Sub MoveAndOpenMsg()
    'Graham Mayor - https://www.gmayor.com - Last updated - 23 Sep 2021
    Dim olItem As MailItem
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set olItem = ActiveInspector.currentItem
            Case olExplorer
                Set olItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        olItem.Move Session.PickFolder
        olItem.Display
    lbl_Exit:
        Set olItem = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks for the help on the code, it works great.
    I should have been more specific in my question.
    I was hoping the vba would move the message to a specific folder named Todosit.

    Thanks

  4. #4
    Where is the folder located?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    It is located Inbox/Todoist

    In addition and totally separate from the question above, I also want to create VBA code to send the message to the Deleted Items folder and then open the message.

    Thanks

  6. #6
    The following should work for you.

    Sub MoveAndOpenMsg()
    'Graham Mayor - https://www.gmayor.com - Last updated - 25 Sep 2021
    Dim olItem As MailItem
    Dim olFolder As Folder
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set olItem = ActiveInspector.currentItem
            Case olExplorer
                Set olItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        Set olFolder = Session.GetDefaultFolder(olFolderInbox).folders("Todoist")
        olItem.Move olFolder
        olItem.Display
    lbl_Exit:
        Set olItem = Nothing
        Set olFolder = Nothing
        Exit Sub
    End Sub
    
    Sub DeleteAndOpenMsg()
    'Graham Mayor - https://www.gmayor.com - Last updated - 25 Sep 2021
    Dim olItem As MailItem
    Dim olFolder As Folder
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set olItem = ActiveInspector.currentItem
            Case olExplorer
                Set olItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        Set olFolder = Session.GetDefaultFolder(olFolderDeletedItems)
        olItem.Move olFolder
        olItem.Display
    lbl_Exit:
        Set olItem = Nothing
        Set olFolder = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Graham,
    Is it possible to have the message deleted first then opened. I believe the current script opens the email and then deletes it. Small difference but it makes a difference.
    The reason is the Todoist plugin I'm using is a task manager. My workflow is:
    -Delete the email
    -Open the email
    -I then run the plugin which sets up a task in Todoist, which remembers the location of the message in Office365.
    -I can then open Todoist and retrieve the email using Office365 webmail.

    Currently your script opens the email then deletes it. Todoist can't find the email as it thinks it should still be in my inbox.

    Hopefully this all makes sense.
    Thanks

  8. #8
    The code moves the message to the deleted items folder then opens it from the deleted items folder
    Set olFolder = Session.GetDefaultFolder(olFolderDeletedItems)     
        olItem.Move olFolder
        olItem.Display
    If you are then running some add-in that expects the message to be in the Inbox, then clearly it won't find it there.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Graham,
    Is your latest code in addition to previous code. I've tried a bunch of variations and my lack of VBA skill I can't seem to get this to work.

    The add-in will expect the email to be in the delete folder when it tries to retrieve the email in the future, so once I can get the code to work I should be good.

    Thanks

  10. #10
    The code you want are the two macros in my reply of the 25th September. The later code snippet is from the DeleteAndOpenMsg code, to illustrate that the message is moved before being opened. It is not a separate macro.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Graham,
    I think my problem is I need to open the message after it has been deleted. As you mentioned the code opens the message before being deleted.

  12. #12
    That is not what I said. The code opens the message after it has been moved, however try the following instead. This closes the selected message then reopens it from the deleted items folder.

    Sub DeleteAndOpenMsg()
    'Graham Mayor - https://www.gmayor.com - Last updated - 30 Sep 2021
    Dim olItem As MailItem
    Dim olFolder As Folder
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set olItem = ActiveInspector.currentItem
            Case olExplorer
                Set olItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        Set olFolder = Session.GetDefaultFolder(olFolderDeletedItems)
        olItem.Move olFolder
        olItem.Close olDiscard
        Set olItem = olFolder.Items.GetLast
        olItem.Display
    lbl_Exit:
        Set olItem = Nothing
        Set olFolder = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  13. #13
    That now works perfect, appreciate the help Graham!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •