Link to home
Start Free TrialLog in
Avatar of CarlK
CarlK

asked on

How do I create Outlook email using VB .NET if Outlook is already open?

I would like to create an email in Outlook from my VB .net application. My code works fine if Outlook is closed but throws an error if Outlook is already open.

Imports Outlook = Microsoft.Office.Interop.Outlook

Dim olApp As Outlook.Application = New Outlook.Application (works OK if Outlook is closed but throws error if Outlook is already open)

ERROR (if Outlook is already open):
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

If I use the CreateObject method save result (it works only if Outlook is not running)

olApp = CType(CreateObject("OUTLOOK.Application"), Outlook.Application)

ERROR (if Outlook is already open):
Cannot create ActiveX component.

How can I create an Outlook email if Outlook is already open? (My app already gives the user the choices to send mail via SMTP and Default Mail but I also want to give the user the option to use Outlook too.)

Avatar of IanTh
IanTh
Flag of United Kingdom of Great Britain and Northern Ireland image

hmm

hope this points in the right direction

http://www.serverwatch.com/tutorials/article.php/1474711/Send-E-mail-Using-Microsoft-Outlook-Automation.htm

as in his intro he says it chaecks if poutlook is open or not so he some code that achieves what your trying to do
Avatar of CarlK
CarlK

ASKER

Thanks, but the GetObject does not work either.

With Outlook OPEN already the line;

olApp = CType(GetObject("", "Outlook.application"), Outlook.Application)

throws a "Cannot create ActiveX component." error.

Perhaps the GetObject/CreateObject methods works differently for Windows 7 and/or Office 2010 because there are many many articles like the one you referenced which show the GetObject and CreateObject as the way to open Outlook, yet if fails for me.
Avatar of CarlK

ASKER

Sorry, If you read the entire thread you'll see that it did not fix the user's problem.
Office 32 or 64bit?
Avatar of CarlK

ASKER

Windows 7 64 bit, Office 2010 32 bit
Avatar of CarlK

ASKER

Okay so here's what I have discovered. In all my previous test I was running my app in the Visual Studio 2010 IDE. The GetObject method doesn't work as expected. If I build the project and run it as a stand alone app (EXE file) the code works fine.  

This code work in the compiled executable

Dim olApp As Outlook.Application = Nothing
  Try
       'try to use existing instance of Outlook
       olApp = CType(GetObject("", "Outlook.application"), Outlook.Application)
   Catch ex As Exception
        'Outlook is not running. Create a new instance
        Try
           olApp = CType(CreateObject("OUTLOOK.Application"), Outlook.Application)
         Catch exc As Exception
            strErrMsg = "Outlook Send mail Error." + vbCrLf + vbCrLf
            strErrMsg += ex.Message
            GoTo sub_Exit
         End Try
   End Try
ASKER CERTIFIED SOLUTION
Avatar of cstaff16
cstaff16

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial