More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 19-Oct-2017 09:10

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: GetCurrentItem() function
Description: Many VBA macros are intended for use on the current item. To avoid writing two different macros -- one for an item selected in a folder and one for an open item -- you can use this code to return the current item from the active window.
Date: 05-Jan-2003  13:43
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
        
    Set objApp = CreateObject("Outlook.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
        Case Else
            ' anything else will result in an error, which is
            ' why we have the error handler above
    End Select
    
    Set objApp = Nothing
End Function
All 29comments
Page [ 1 2 3 Next >>  
  09-Jan-2003  18:25   
Can you modify the above to select all items in the current folder? I need to find a way to select all custom task items in a Public Folder and copy them to Excel. Ideally I would like this to happen automatically every 15 minutes. Any suggestions?
  14-Jan-2003  21:34   
The Selection collection returns the items the user has selected in the user interface. If you need all the items, there's no need to use Selection. Just loop through the Items collection for the folder using a For Each ... Next loop.
  31-Aug-2003  12:10   
Hi Sue,

  I purchased and very much enjoyed your book. I've also been checking out Slipstick.com to try to solve a problem I'm having. Perhaps you could suggest a solution for me.



My company uses Outlook 2000 to recieve emails every hour from specific PC's across the country. We use the rulse wizard to forward each PC's email to its own folder and then manually check each folder each hour to see if the PC "checked" in. I'm interested an app or writing some code that can do this automatically and alert us if a specific PC fails to check in within an hour or two. Any susggestions would be greatly appreciated.
  20-May-2004  11:40   
Sue,

I use the code above to process the text of the current item in either a Task or Calendar object. The problem I have is if someone has the Calendar view up which also has the TaskPad displayed, and they put focus on a task in the TaskPad, I don't seem to get task item returned, but instead the calendar item. Is there a way to get the Task item from the TaskPad if it has focus?
  21-May-2004  07:08   
No. The Selection object applies only to the Explorer window displaying the current folder. The TaskPad subwindow and its selection is not accessible programmatically.
  25-Oct-2004  13:21   
Since this is a Function, will it work in a macro that I can put on the toolbar? I am trying to create a button that will move the selected items to a specific .pst in Outlook 2000 and think this would be useful.
  26-Oct-2004  12:01   
This function can called in a macro to return the item that you want the macro to perform some operation on. If you wanted to process multiple selected items, you would use not this function but the ActiveExplorer.Selection collection.
  15-Nov-2004  09:25   
Hi Sue, Used this code no problems to create a custom replyto macro. It works but I have a problem. I have a rule running that notifies me when a message arrives (notify me using a specific message). If I open the message from this dialogue, then click on my custom reply macro, the reply is not generated from that window but the item that is highlighted in the main outlook window. Is there anyway to make sure it picks up the open window?
  17-Nov-2004  09:07   
What version of Outlook, Baz?
  23-Nov-2004  17:16   
Sorry for the delay. 2002 SP1 I think.....
Page [ 1 2 3 Next >>