ARTICLE

Automate Send & Receive in Outlook

Posted by Sridhar Manoharan Articles | Office and VB.NET August 06, 2007
In this article we will take the privilege of outlook and make it to send & receive the mail automatically using VB.Net.
 
Reader Level:

Introduction:

 

This article clearly explains about how to automate or force the Send/Receive event in the outlook using vb.net. This code can be used as a service and made to automate the Send/Receive event in the outlook for a regular interval.

 

Summary:

         

The following steps will help us to create the application.

 

Step 1: Start Microsoft Visual Studio .NET.

 

Step 2: On the File menu, click New, and then click Project.

 

Step 3: Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.

 

Step 4: Add a reference to the Microsoft Outlook 10.0/11.0 Object Library. To do this, follow these steps: 

  • On the Project menu, click Add Reference.
  • On the COM tab, click Microsoft Outlook 10.0/11.0 Object Library, and then click Select.
  • Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the library that you selected, click Yes.

Step 5: In the Code window, replace all of the code with the following:

 

        Automate Send & Receive:

 

Imports System.Reflectio

Module Module1

    Sub Main()

        Try

            'Microsoft.Office.Interop.Outlook._Application

            ' Create an Outlook application.

            Dim oApp As Outlook._Application = New Outlook.Application

            ' Get the MAPI NameSpace and Logon values.

            Dim oNS As Outlook._NameSpace = CType(oApp.GetNamespace("MAPI"),

             outlook._NameSpace)

            oNS.Logon(Missing.Value, Missing.Value, True, True)

 

            'Get Explorer for the folder.

            Dim oExp As Outlook._Explorer = oFolder.GetExplorer(False)

 

            'Get the Menu bar.

            Dim oCmdBars As office._CommandBars = oExp.CommandBars

            Dim oCmdBar As office.CommandBar = oCmdBars("Menu Bar")

            Console.WriteLine(oCmdBar.Name)

            Dim oBarCrls As office.CommandBarControls = oCmdBar.Controls

            Console.WriteLine(oBarCrls.Count)

            Dim oBP As office.CommandBarPopup

 

            'Get the Tools menu.

            Dim oBPop As office.CommandBarPopup = CType(oBarCrls("Tools"), 

            office.CommandBarPopup)

            Console.WriteLine(oBPop.Caption)

            oBarCrls = oBPop.Controls

            Dim oBn As office.CommandBarControl

 

            'Get the Send/Receive menu.

            Dim oSendReceive As office.CommandBarPopup =  

            CType(oBarCrls("Send/Receive"), office.CommandBarPopup)

            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.

 

            'Dim oSendReceiveAll As Office.CommandBarControl =

            'CType(oBarCrls("Send and Receive All"), Office.CommandBarControl)

 

            'TO DO: If you use the Microsoft Outlook 11.0 Object Library,

            ' uncomment the following line.

 

            'Dim oSendReceiveAll As Office.CommandBarControl =

            ' CType(oBarCrls("Send/Receive All"), Office.CommandBarControl)

 

            Console.WriteLine(oSendReceiveAll.Caption)

 

            'Do the action.

            oSendReceiveAll.Execute()

 

            'Log off.

            oNS.Logoff()

 

            'Clean up.

            oApp = Nothing

            oNS = Nothing

            oFolder = Nothing

            oExp = Nothing

        Catch e As Exception

            Console.WriteLine("{0} Exception caught.", e)

        End Try

    End Sub

 

Step 6: Press F5 to build and run the program.

 

Step 7: Verify that the Contact Detail (contact detail in outlook format) is received through mail

 

Conclusion:

 

This process even can be achieved by inbuilt schedule option of the Microsoft Outlook. If it was done it needs the outlook to be open but in our case its not needed and the same is achieved through the vb.net coding.

Login to add your contents and source code to this article
share this article :
post comment
 

Visual Studio is not recognizing ofolder. Any suggestions?

Posted by tharpa Nov 15, 2007

Hi, Iam using the following code for the "subject alert" , it is working fine for that session only, whenever restarting outlook, iam losing the code..........., can u help me out in the issue like how can i save that code for the entire session in the out look...... Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim strSubject As String strSubject = Item.Subject If Len(strSubject) = 0 Then Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?" If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then Cancel = True End If End If End Sub Thanks & regards, R.Mohan.

Posted by ram mohan Aug 24, 2007
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor