Automate Send & Receive in Outlook Using VB.NET

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.

Up Next
    Ebook Download
    View all
    Learn
    View all