.netvb.netoutlookoffice-interop

Unable to cast COM object of type Outlook in vb.net


In my VB.Net WinForms Application which is upgraded from framework 1.1 to 4.7 (Visual Studio 2022, Framework 4.7), I am using Outlook interope dll to send mails.

enter image description here

These are the dlls that i have added in my project.

enter image description here

And the namespace imported as shows

    Imports System.Collections.Generic
    Imports System.Globalization
    Imports System.IO
    Imports System.Text.RegularExpressions
    Imports System

And the interope dll that i copied from below location.

enter image description here

When I give the full namespace, the ambiguous error is gone but another error threw as shown below:

    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: Element 
    not found. (Exception from HRESULT: 0x8002802B 
    (TYPE_E_ELEMENTNOTFOUND)).

What is this error ??? How to solve ???

The error is thrown here....

     Dim app As Microsoft.Office.Interop.Outlook.Application = New 
     Microsoft.Office.Interop.Outlook.Application()

     Dim newMail As Microsoft.Office.Interop.Outlook.MailItem = 
    CType(app.CreateItem(Microsoft.Office.Interop.Outlook.
    OlItemType.olMailItem), 
    Microsoft.Office.Interop.Outlook.MailItem) // error threw here

I tried below 2 ways.

    Dim newMail As Microsoft.Office.Interop.Outlook.MailItem = DirectCast(app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Microsoft.Office.Interop.Outlook.MailItem)

    Dim newMail As Microsoft.Office.Interop.Outlook.MailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)

But still threw error


Solution

  • I have solved the issue. I commented this line

       'Dim app As New Microsoft.Office.Interop.Outlook.Application
    

    And added below code

        Dim app As Object
        app = CreateObject("Outlook.Application")
    

    It worked.