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

I have created a 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 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)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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 = ""
            .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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BOITAuthor Commented:
Thanks for the prompt and accurate response Adam Barrett this works perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.