How to use the Microsoft Outlook Object Library to force a Send or Receive action by using Visual C# (310251)



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002
  • Microsoft Visual C# 2005

This article was previously published under Q310251

INTRODUCTION

This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to force a Send/Receive action by using Microsoft Visual C#.

MORE INFORMATION

To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to force a Send/Receive action in a Visual C# project, follow these steps:
  1. In Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, create a new Console Application project:
    1. On the File menu, click New, and then click Project.
    2. Under Project Types, click Visual C# Projects.

      Note In Visual C# 2005, Visual C# under Project Types.
    3. Under Templates, click Console Application.
    4. Click OK. By default, a file that is named Class1.cs is created.

      Note In Visual C# 2005, a file that is named Program.cs is created by default.
  2. Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. On the COM tab, click Microsoft Outlook 11.0 Object Library if you are using Outlook 2003, or click Microsoft Outlook 10.0 Object Library if you are using Outlook 2002.
    4. Click Select.

      Note In Visual C# 2005, you do not have to click Select.
    5. In the Add References dialog box, click OK.

      Note If you receive a message to generate wrappers for the libraries that you selected, click Yes.
  3. In the Class1.cs code window, replace all the existing code with the following code:
    namespace OOM
    {
    	using System;
    	using System.Reflection;     // to use Missing.Value
    	using Office = Microsoft.Office.Core; // to use Menu and Toolbars
    
    	//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    	//using Outlook = Microsoft.Office.Interop.Outlook;
    
    	public class Class1
    	{
    		public static int Main(string[] args)
    		{
    			try 
    			{	//Microsoft.Office.Interop.Outlook._Application
    				// Create an Outlook application.
    				Outlook._Application  oApp = new Outlook.Application();
          
    				// Get the MAPI NameSpace and Logon values.
    				Outlook._NameSpace  oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");
    				oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
    				// Get the Inbox.
    				Outlook.MAPIFolder oFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    
    				// Get Explorer for the folder.
    				Outlook._Explorer oExp = oFolder.GetExplorer(false);
          
    				// Get the Menu bar.
    				Office._CommandBars oCmdBars = oExp.CommandBars;
    				Office.CommandBar oCmdBar = oCmdBars["Menu Bar"];
    				Console.WriteLine(oCmdBar.Name);
    
    				Office.CommandBarControls oBarCrls =  oCmdBar.Controls;
    				Console.WriteLine(oBarCrls.Count);
    
    				foreach(Office.CommandBarPopup oBP in oBarCrls)
    				{
    					Console.WriteLine(oBP.Caption);
    				}
    
    				// Get the Tools menu.
    				Office.CommandBarPopup oBPop = (Office.CommandBarPopup)oBarCrls["Tools"];
    				Console.WriteLine(oBPop.Caption);
    
    				oBarCrls = oBPop.Controls;
    
    				foreach(Office.CommandBarControl oBn in oBarCrls)
    				{
    					Console.WriteLine(oBn.Caption);
    				}
    
    				// Get the Send/Receive menu.
    				Office.CommandBarPopup oSendReceive = (Office.CommandBarPopup)oBarCrls["Send/Receive"];
    				Console.WriteLine(oSendReceive.Caption);
    
    				// Get the Send and Receive All menu.
    				oBarCrls = oSendReceive.Controls;
    				//TO DO: If you use the Microsoft Outlook 10.0 Object Library, uncomment the following line.
    				//Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls["Send and Receive All"];
    				
    				//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    				//Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls["Send/Receive All"];
    				Console.WriteLine(oSendReceiveAll.Caption);
    
    				// Do the action.
    				oSendReceiveAll.Execute();
    
    				// Log off.
    				oNS.Logoff();
          
    				// Clean up.
    				oApp = null;
    				oNS = null;
    				oFolder = null;
    				oExp = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}  
    
    			return 0;
                         
    		}
    	}
    
    }
  4. In this code, make any necessary changes where you see the "TO DO" comments.
  5. Press F5 to build and then run the program.

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbcode kbCtrl kbinterop kbOutlookObj kbhowto KB310251 kbAudDeveloper