none
CreateObject("Outlook.application") does not work in VB.net but does in VBA RRS feed

  • Question


  • I am trying to create an email from VB.net. The code is as follows  Imports Outlook = Microsoft.Office.Interop.Outlook

    Public Class InputForm

    Private Sub EmailTeam_Click(sender As Object, e As EventArgs) Handles EmailTeam.Click

     

            Dim Subject As String

            Dim Recipient As String

            Dim Message As String

            Subject = "My Team"

            Recipient = "myemail@address"

            Message = " Team details"

            SendOutlookMail(Subject, Recipient, Message)

        End Sub

        Public Sub SendOutlookMail(Subject As String, Recipient As _

    String, Message As String)

     

            On Error GoTo errorHandler

            Dim oLapp As Outlook.Application

            Dim oItem As Object

          

            oLapp = CreateObject("Outlook.application")

            oItem = oLapp.CreateItem(0)

     

            With oItem

                .Subject = Subject

                .To = Recipient

                .body = Message

                .Display()

                '.Send()

            End With

     

            oLapp = Nothing

            oItem = Nothing

     

            Exit Sub

     

    errorHandler:

            oLapp = Nothing

            oItem = Nothing

            Exit Sub

     

        End Sub

    End Class

      But when it gets to  oLapp = CreateObject("Outlook.application") it jumps to the errorHandler: and exits the sub routine. Why does CreateObject("Outlook.application") not work?

    I do not get an error message but when I have run my code I get this output.   'MatchManagement.vshost.exe' (CLR v4.0.30319: MatchManagement.vshost.exe): Loaded 'D:\My Documents on D\Visual Studio Projects\Match Management Application\MatchManagement\bin\Debug\MatchManagement.exe'. Symbols loaded. 'MatchManagement.vshost.exe' (CLR v4.0.30319: MatchManagement.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Cannot find or open the PDB file. 'MatchManagement.vshost.exe' (CLR v4.0.30319: MatchManagement.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'. Cannot find or open the PDB file. 'MatchManagement.vshost.exe' (CLR v4.0.30319: MatchManagement.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. A first chance exception of type 'System.Exception' occurred in Microsoft.VisualBasic.dll The program '[4464] MatchManagement.vshost.exe' has exited with code -1 (0xffffffff).

    Whilst debugging I set a breakpoint and used F11 to step through code. Having got to the errorhandler I moved the arrow back to the start of the subroutine and carried on stepping.  On reaching the line oLapp = CreateObject("Outlook.application") I got the following error; An unhandled exception of type 'System.Exception' occurred in Microsoft.VisualBasic.dll   Additional information: Cannot create ActiveX component.

    It is annoying but the same line of code works in VBA and Excel.

    Dave

    Friday, March 20, 2015 11:09 AM

Answers

  • Hi Eugene

    I load Office 365  whilst I still had 2007 Office loaded. Once I was happy with 365 Office I unloaded 2007.

    It seems this damages Office 365.

    Run a repair on Office 365 and all is working now.

    Thanks for your help.

    Friday, March 20, 2015 10:06 PM

All replies

  • Hello Dave,

    Take a look at the How to automate Outlook from another program article which describes different ways for creating a new Application instance. Did you have a chance to go the early-binding way?

    For example:

    oLapp = CreateObject("Outlook.application")

    Try to use the following line of code instead:

    oLapp = New Outlook.Application

    What Outlook version do you have installed on the problematic PC? Do you have the ClickToRun edition of Office installed on the problematic PC?

    Please take a look at the  Is OLE automation possible with virtualized Office versions ?  forum thread. It states: Click2Run apps have no automation interface and cannot be automated.

    Finally, the You receive run-time error 429 when you automate Office applications article describes a similar issue in depth. Also see Cannot create ActiveX Component .

    Friday, March 20, 2015 11:30 AM
  • What is the bitness of Outlook and your app?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 20, 2015 4:38 PM
  • Eugene

    Hi, Tried using 

    oLapp = New Outlook.Application

    but got this error messagefrom the next line of code;  oItem = oLapp.CreateItem(0)

    An unhandled exception of type 'System.InvalidCastException' occurred in MatchManagement.exe

    Additional information: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).

    I have Office 365 loaded on my PC. The app I am developing is a copy of the one I have already developed using VBA and Excel on this PC and it works OK including creating emails in outlook. I have moved to VB.Net so I can be independent of Excel( using XML files to save the data)

    Friday, March 20, 2015 9:23 PM
  • Hi Dmitry

    I am running on 64 bit machine with 365 Office and Visual Studio Community 2013

    Just found out the Outlook 2013 is 32 bit

    Friday, March 20, 2015 9:27 PM
  • Hi Dmitry

    Run a repair on Office 365 and all is working now.

    Thanks for your help.

    Friday, March 20, 2015 10:04 PM
  • Hi Eugene

    I load Office 365  whilst I still had 2007 Office loaded. Once I was happy with 365 Office I unloaded 2007.

    It seems this damages Office 365.

    Run a repair on Office 365 and all is working now.

    Thanks for your help.

    Friday, March 20, 2015 10:06 PM