Microsoft KB Archive/310251

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

Article ID: 310251

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * 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:  In Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, create a new Console Application project:  On the File menu, click New, and then click Project. Under Project Types, click Visual C# Projects.

Note In Visual C# 2005, Visual C# under Project Types. Under Templates, click Console Application. 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.  Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library:  On the Project menu, click Add Reference.</li> Click the COM tab.</li> 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.</li> Click Select.

Note In Visual C# 2005, you do not have to click Select.</li> 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.</li></ol> </li>  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(&quot;MAPI&quot;); 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[&quot;Menu Bar&quot;]; 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[&quot;Tools&quot;]; 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[&quot;Send/Receive&quot;]; 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[&quot;Send and Receive All&quot;]; //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. //Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls[&quot;Send/Receive All&quot;]; 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(&quot;{0} Exception caught.&quot;, e); }

return 0; }   }

} </li> In this code, make any necessary changes where you see the &quot;TO DO&quot; comments.</li> Press F5 to build and then run the program.</li></ol>

Keywords: kbcode kbctrl kbinterop kboutlookobj kbhowto KB310251

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.