PDA

View Full Version : Outlook VBA - move message then open



maclachlan19
09-22-2021, 10:04 AM
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

gmayor
09-23-2021, 01:32 AM
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

maclachlan19
09-23-2021, 05:51 AM
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

gmayor
09-23-2021, 08:49 PM
Where is the folder located?

maclachlan19
09-24-2021, 05:20 AM
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

gmayor
09-24-2021, 08:45 PM
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

maclachlan19
09-27-2021, 10:50 AM
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

gmayor
09-27-2021, 09:04 PM
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.

maclachlan19
09-29-2021, 04:32 PM
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

gmayor
09-29-2021, 08:57 PM
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.

maclachlan19
09-30-2021, 01:54 AM
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.

gmayor
09-30-2021, 03:05 AM
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

maclachlan19
09-30-2021, 03:18 AM
That now works perfect, appreciate the help Graham!!