How do I add a reference to MS Office at runtime in a VB.NET application?

Posted on 2009-05-27
Medium Priority
Last Modified: 2012-05-07
I have created a vb.net application with Visual Basic 2008 express which among other things allows the user to link to Outlook to send emails to addresses in a SQL Server database.
Some users have Office 2003 and some have Office 2007.

In a previous application a wrote in Microsoft Access I was able to add a reference the correct Outlook library at runtime by checking which Microsoft Office was installed. However vb.net seems to insist on have the references linked before compile.

Is there any way to overcome this?

(all outgoing email on these machines must go via the client's Outlook configuration and Exchange, it is not permissible to directly SMTP the emails)
Question by:BOIT

Accepted Solution

Adam_Barrett earned 2000 total points
ID: 24482020

I had this problem a few years ago. The solution is to use late binding instead of early binding. With early binding you set a reference to your version of office within visual basic/ access when you are creating your application. With late binding you dont add any references and therefore it wont matter which version of office you are running it on.

The simple answer is to:

1)Remove your references to outlook/ office.
2)Next use this code to create your email

        Dim olApp, olMailMessage
        olApp = CreateObject("Outlook.Application")
        olMailMessage = olApp.CreateItem(0)
        With olMailMessage
            .To = "a@b.com"
            .Subject = "Test"
            .Body = "Testing"
        End With
        olMailMessage = Nothing
        olApp = Nothing

You can also use .display instead of .send if you wish to preview your email first in outlook.

Hope this helps


Author Comment

ID: 24482159
Thanks for the prompt and accurate response Adam Barrett this works perfectly.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question