Ziff Davis EnterpriseHeader
Advertisement
Advertisement
Advertisement
Friday, April 27, 2007 10:49 AM/EST

Communicating with Outlook in a .NET program

Hi everyone!

I thought I'd share with you a quick tip that demonstrates how your .NET programs can interact with Microsoft Office programs. In this example, I'll use Outlook.

Start up Visual Studio and create a new Windows Application. Here I'm using VB.NET, although you can use C# too, of course. In Solution Explorer, right-click on your project, and choose Add Reference... In the Add Reference dialog, click the COM tab, as shown here. Scroll down and find Microsoft Outlook Object Library. (There will also be a version number present.)
AddReferenceCOM.jpg
Then click OK. (In a separate blog posting I'll talk a bit about what exactly just happened when you added a COM reference to your project.)

Now go to your form in Design view. Drag a button and a text box onto it. Set the text box's Multiline property to True. Your form will look something like this:

OutlookForm1.jpg

Double-click the button to add a click handler. When the Form1.vb code opens, scroll to the top of the file and add the following Imports statement, which will make it easier to access the Outlook type names:

Imports Microsoft.Office.Interop

Then add the following code inside the handler, shown in bold:

Imports Microsoft.Office.Interop Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim OLapp As New Outlook.Application() Dim OLNS As Outlook.NameSpace = _ OLapp.GetNamespace("MAPI") Dim OLFolder As Outlook.MAPIFolder Dim OLMailItem As Outlook.MailItem For Each OLFolder In OLNS.Folders TextBox1.Text &= OLFolder.Name & vbCrLf Dim i As Int32 Dim max As Int32 max = 10 If OLFolder.Items.Count < 10 Then max = OLFolder.Items.Count End If For i = 1 To max OLMailItem = OLFolder.Items.Item(i) TextBox1.Text &= " " & OLMailItem.Subject _ & vbCrLf Next Next OLapp.Quit() End Sub End Class

The first line inside the handler opens a new instance of Outlook, and saves a reference to the application in the variable called OLapp. The next line grabs a namespace for your outlook objects. The next line declares a folder variable. Then the for loop loops through all the folders. The inner loop loops through the first 10 messages in the folder, displaying the subject lines.

Each item is written to the text box.

Note that if you're curious about what members are available, you can use the Intellisense in Visual Studio. For example, this image shows me checking what members are available to the mail item:

OutlookIntellisense.jpg

You can also use Intellisense to see what data types are available, like so:

OutlookIntellisense2.jpg

If you find this interesting or useful, I suggest playing around and trying out the various types and objects and see what you can do. Be careful, though! You're working with the live data, and if you make changes you could lose all your email. (I recommend backing up your Outlook data first.) Have fun!

TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/10856

Post a Comment

 
 
Advertisement

Syndication

Subscribe: