To work with the currently selected item (not opened) in the message list, you will need to reference the ActiveExplorer and will use the ActiveExplorer method to get the item in the Explorer window. (This is used for all Outlook items that are selected but not opened: appointments, contacts, tasks, etc.)
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
When you are working with an Outlook item that is open and has focus, it uses the ActiveInspector method. The code you need to work with Inspectors is.
Set objItem = objApp.ActiveInspector.CurrentItem
To help remember which is which, think of the main Outlook window as Windows Explorer. When you work with items in this window, you'll use Explorer.
Use macros with both opened and selected items
When you want to use the macro with both open or selected items, you'll use this function to determine if the item is open or selected. You'll call the function in your code in this manner:
Set objItem = GetCurrentItem()
You'll need to put one copy of this function in a module and can use the function in many macros.
Function GetCurrentItem() As Object Dim objApp As Outlook.Application Set objApp = Application On Error Resume Next Select Case TypeName(objApp.ActiveWindow) Case "Explorer" Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) Case "Inspector" Set GetCurrentItem = objApp.ActiveInspector.CurrentItem End Select Set objApp = Nothing End Function
These articles are applicable to all versions of Outlook:
More Information
Application.ActiveInspector Method
Application.ActiveExplorer Method
Thanks for the great explanation and code examples. I have your GetCurrentItem function running but using ".Parent" on it only gives me the name "inbox", But I need the object (and name) of the Mailbox this item is in (or the Mailbox currently being processed), because my Outlook .OST file has about a dozen mailboxes from which it gathers mail. Do you have any examples or hints on how to get that name WITHOUT LOOPING, because I need to use it for each mail item being processed (I get 100's/day) and will use this as an Outlook Rule to move the item to a folder in the current mailbox.
Hi Diane,
can you give an example script for minimize all opened items (appointments) at once?
I want to create custom button in outlook ribbon so when I hit it can minimize all the opened appointment window.
Thank you
Bonjour,
Super fonction et mille mercis
Comment modifier cette fonction pour travailler sur un mail en cours d'édition ?
Je vous remercie
Meilleures salutations
Philippe
https://www.developpez.net/forums/d2121684/logiciels/microsoft-office/outlook/suivi-d-message-outlook/#post11788404
Bonjour,
Comment récupérer le nom du dossier sélectionné dans un mail en cours d'édition ?
Voir image
Je vous remercie
Meilleures salutations
Philippe
You need to use getinspector. Depending on what you want to do, you might need to use word object model and inspector.
This has a sample - it formats text, but you can use the same method to insert text or the clipboard contents..
Use Word Macro to Apply Formatting to Email (slipstick.com)
Hello Diane,
is there any way to know, in Inspector case, whether the email has been opened from the hard disk or from Outlook?
Thank you.
Yohann
No, not to my knowledge. You can get the parent - it will always be inbox for messages opened from the file system - but that may not be all that helpful if you are running it on items in the inbox too. .
Thank you for the reply.
Totally... there is no differentiation. It appears as it was in the inbox.
I wanted to know that because I propose to user to delete the email from the mailbox once he has exported it in msg format (it is an saving tool); the tool can also be used for an email opened from the file system (for example to make a copy in another folder), but in that latter case, if the user chooses to delete the email source in the mailbox, it generates an error as the email does not come from the mailbox but from the file system.
I get around the problem in resuming the deletion in case of error. It is better than nothing.
Hi Diane,
Thanks for all your help in the past!
Using this GetCurrentItem code has been great for me except for one thing I can't figure out.
If in Explorer, a conversation is selected by the header row of that conversation, this function fails to find the item (or any item) within the conversation. I tried something like:
Set GetCurrentItem = objApp.ActiveExplorer.Selection.GetSelection(olConversationHeaders).GetConversation.Item(1)
but haven't gotten too far. Can you help?
Thx,
I've used this to validate attachment sent outside of my organization. The problem I have with using Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) is if I forward an email with attachments, then delete one or all of those attachments, the script still thinks the attachments are there.
Is there a workaround for that?
Diane,
I know this is an OLD issue, but I still use the Journal and want to get this to work inside Outlook. I am a neophyte in VBA, but I managed to add Modules for CreateJournalForcontact and I also have a Module for GetCurrentItem as per your instructions, but I continue to get the "Sorry you need to select a contact" error. Can you help me figure out what I may have done wrong???