Solved

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

Posted on 2011-03-13
8
3,723 Views
Last Modified: 2012-05-11
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.)

0
Comment
Question by:CarlK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 35123667
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
0
 

Author Comment

by:CarlK
ID: 35123747
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.
0
 
LVL 30

Expert Comment

by:IanTh
ID: 35123916
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CarlK
ID: 35124145
Sorry, If you read the entire thread you'll see that it did not fix the user's problem.
0
 
LVL 4

Expert Comment

by:dailypcguy
ID: 35125081
Office 32 or 64bit?
0
 

Author Comment

by:CarlK
ID: 35128494
Windows 7 64 bit, Office 2010 32 bit
0
 

Author Comment

by:CarlK
ID: 35143000
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
0
 
LVL 1

Accepted Solution

by:
cstaff16 earned 500 total points
ID: 35204216
I was encountering the same issue in an App and figured out what was causing it.
On a Vista/Win 7 machine if you have UAC turned on and your process is started with elevated privilages (Run as Administrator), but outlook is started normal (without Run as Administrator), then you cannot CoCreate an instance of it.  The only solution we came up with was to run both at the same level or turn of UAC if that is an option.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

732 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