VB.Net Fails to open Outlook object, where VBA succeeds

I have a piece of equivalent VBA and VB.Net code that opens an Outlook object and sends an email.  I wrote the VBA within Outlook and Excel; and both work.  The equivalent VB.Net code does not, and that's what where I'm writing the application.  For some inexplicable reason, both of these lines of code fail as VB.Net code:

MyOutlook = CreateObject("Outlook.Application")

MyOutlook = GetObject(, "Outlook.Application")

Open in new window


Any ideas?  ~Peter Ferber
PeterFrbWeb development, Java scripting, Python TrainingAsked:
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.

CodeCruiserCommented:
Do you get an error?
0
Miguel OzSoftware EngineerCommented:
How to create Application instance using  Outlook Interop:
using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application MyOutlook =  new Outlook.Application();

Open in new window


The interop libraries are part of .NET 4.0 Framework.
0
PeterFrbWeb development, Java scripting, Python TrainingAuthor Commented:
Hello, and thanks for the reply.

I'm not familiar with the "Using" clause in VB.Net, so I need a little more context in order to implement it.   Having tried it, I get an error message that an "End Using" is required, but nothing I do will get the code not generating an error.  

When using the code below, I get the error message, "cannot generate ActiveX component".  I have a copy of Outlook open at the time I write the code, and my VBA code written from Excel succeeds in getting the application to send an email, with a CC, BCC, and, if desired, an attached document.  Hope this helps.  ~Peter Ferber

Dim objOutlook As Microsoft.Office.Interop.Outlook.Application
objOutlook = GetObject(, "Outlook.Application")

Open in new window

0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

PeterFrbWeb development, Java scripting, Python TrainingAuthor Commented:
And here's more feedback: I have VB6 on my machine, and I tried using that to open Outlook, but I get the same error when trying to open with VB6: "cannot generate ActiveX component".  Excel and Outlook are both part of the Microsoft Office Suite, and it seems that the program works as long as I'm using an application that fits within that umbrella.  But going outside that umbrella, as VB6 and VB.Net do, my attempts have thus far failed.  I hope this leads to an insight.  Thanks, ~Peter Ferber
0
PeterFrbWeb development, Java scripting, Python TrainingAuthor Commented:
Yet more feedback:  I discovered that if Outlook is closed, a "CreateObject("Outlook.Application")" does work, and the program sends an email.  The problem lies when the application is already open.  And when .Net succeeds in sending, the application remains invisible.  I have to open Outlook to see that the program actually did what I asked.  While this is progress, the condition remains far less than optimal.  ~Peter Ferber
0
Miguel OzSoftware EngineerCommented:
Sorry, my code was in C#, the VB.net equivalent is:
Dim objOutlook As Microsoft.Office.Interop.Outlook.Application
objOutlook = new Microsoft.Office.Interop.Outlook.Application()

Open in new window

The code above only works for .NET not for VB6.
REgarding your previous comments what version of Office are you using?
Your Microsoft.Office.Interop.Outlook reference must be the one recomended for your target Office suite. For example, the  Interop.Outlook  version 12 is for Office 2007 only and version 14 is for Office 2010 only.
0
PeterFrbWeb development, Java scripting, Python TrainingAuthor Commented:
I'm using Office 2010.  My .Net referece is to the "Microsoft Outlook 14.0 object library".  The only difference between my original code and your updated code is that I didn't have parantheses after my declaration.  I tried that, but it still does not work.

What I've done thus far is to divert to an error message, alterting the user to close the open version of Outlook before continuing.  I feel fortunate to have discovered that, with Outlook closed, .Net does work.  The solution, again, is kludgy but workable.  The system still does not link to an already-instantiated Outlook object.  Strange but true.  ~Peter Ferber
0
Miguel OzSoftware EngineerCommented:
Is the code above running  in a winform/console/asp.net app?
Is the code above running  in the main execution thread?

The interop code only runs on the main execution thread, otherwise behaviour is not guaranteed.
The posted solution opens a new instance of Outlook, but you may be missing clean up code:
objOutlook = Nothing
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

Open in new window


Notes:
1) In VBA this happens automatically, but in .NET we need to clean up when closing our program or the interaction is no longer needed because the Office interaction uses COM technology. Notice that you must set to Nothing every single outlook-based instance after you finish using it.
2) You may need to restart your PC (or kill all Outlook instances using task manager before testing your code again)
0
CodeCruiserCommented:
Try changing your code to following

MyOutlook = GetActiveObject("Outlook.Application")

If IsNothing(MyOutlook) Then
   MyOutlook = CreateObject("Outlook.Application")
End If
0

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
PeterFrbWeb development, Java scripting, Python TrainingAuthor Commented:
Excellent answer: succinct and covers all bases.  Thanks!
0
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.NET

From novice to tech pro — start learning today.