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
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???
For me to get this to work I had to remove objApp. I.e. I changed this:
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
to this:
Set objItem = ActiveExplorer.Selection.Item(1)
Otherwise I got some error, I believe run-time error 424 Object required
Hi Diane.
I have a requirement of Reading subject line of a mail in my Microsoft outlook inbox from excel. Once i receive a mail of the desired subject line, it has to trigger my database query through excel.
I need a help on reading a mail from outlook inbox and triggering my Microsoft excel macro for running database query.
Thanks.
I think what you want to do is watch the folder in outlook then send it to Excel - outlook can run the query or it may be able to trigger a macro in Excel. The macro at https://www.slipstick.com/developer/vba-copy-outlook-email-excel-workbook/ shows how to interact with excel from outlook.
Thanks for this. I've had to use it to get round a stupid "design" feature in Outlook 2016 where doing a save all attachments doesn't now prompt you to overwrite existing files. This results in multiple versions of downloaded files. Using your code I've created a macro that appears on the ribbon and our users can then click it and it will download the attachments and overwrite any with the same name. Not always what people might want but exactly what this particular application needed. Many thanks.
Hi Diane.
I don't have access to Outlook VBA due to onsite security, but I do to Excel VBA. What changes would I need to make to enable this to run from Excel. PS. It would be handy if it could cycle through all items in the calendar writing the recipients to a spreadsheet.
Thanks
Really? One is as safe as the other. :) You basically just need to fix the references to read outlook from excel. The macro at https://www.slipstick.com/developer/code-samples/macro-export-outlook-fields-excel/ has an excel version (linked to a text file) - while it can easily be tweaked to work on any folder. If you don't need recurring events, that should actually work.
I'll take a look at it tonight - it shouldn't be too much effort to convert it. (Will add the macro to https://www.slipstick.com/developer/list-meeting-attendees-responses/ when it's updated).
Hi Diane,
The .ActiveExplorer.Selection.Item(1) doesn't work when using it together with Outlook send button. I'm trying to add something on the body after clicking the send button. What it does is send the email then change the currently selected item's body.
Regards,
Jake
For that, you need to use an itemsend macro. There is an example here that adds attachment names to the body:
https://www.slipstick.com/developer/code-samples/add-attachments-names-to-message-before-sending/