unable to cast COM object of type microsoft.office.interop.outlook.applicationclass to interface type Microsoft.Office.Interop.Outlook._Application

0

Perform a development where it is necessary for me to obtain the mail that is registered in the Outlook session in Windows. In that sense, the only thing that the user has to do is enter their windows session password to be able to log in and then go to a SharePoint and download several files. At the time of executing the programming, it sends me the following error.

Please verify whit the support the next error: Unable to cast COM object 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 ID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

I already checked in regedit, but the subkey is at 0, I investigated this a little in other internet sites but I still have the same problem. Can you help me to see where the error is? This is the code that I developed.

The Outlook version is 2016 (O365) and Windows 10.

Imports System.Security
Imports Microsoft.SharePoint.Client
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports System.IO

Imports Microsoft.SharePoint
Imports System.Net
Imports System.Net.Http
Imports Microsoft.ProjectServer.Client
Imports System
Imports System.Management
Imports System.Text
Imports Outlook = Microsoft.Office.Interop.Outlook

Module UpdateDB
    Dim password As String
    Dim url As String
    Dim username As String
    Dim ctx As ClientContext

    Dim securedPassword

    Public Function updateDb(value As Integer)
        Try

            ActivateOL()

            Dim outlook As Outlook.Application = New Outlook.Application()

            Dim addrEntry As Outlook.AddressEntry = outlook.Session.CurrentUser.AddressEntry

            If addrEntry.Type = "EX" Then
                Dim currentUser As Outlook.ExchangeUser = outlook.Session.CurrentUser.AddressEntry.GetExchangeUser()

                If currentUser IsNot Nothing Then
                    Dim sb As StringBuilder = New StringBuilder()
                    username = currentUser.PrimarySmtpAddress
                End If
            End If

            Dim siteUrl As String = "https://name_company.sharepoint.com/sites/test/"

            password = Form1.MyInputBox("Please Enter Your Password")


            Form1.ProgressBar1.Minimum = 0
            Form1.ProgressBar1.Maximum = 28
            Form1.ProgressBar1.Visible = True
            Form1.Label115.Visible = True
            Form1.Label115.BringToFront()
            Form1.ProgressBar1.Value = 0


            url = "https://name_company.sharepoint.com/sites/test/SiteFiles/test.txt"

            ctx = New ClientContext(url)

            securedPassword = New SecureString()
            For Each c In password.ToCharArray()
                securedPassword.AppendChar(c)
            Next


            My.Computer.FileSystem.DeleteFile("C:\Temp\test.txt")
            ctx.Credentials = New SharePointOnlineCredentials(username, securedPassword)
            DownloadFile(url, ctx.Credentials, "C:\Temp\test.txt")
            Form1.ProgressBar1.Value = 1
        Catch ex As Exception
            MsgBox("Please verify whit the support the next error: " & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "TEST")
            Form1.ProgressBar1.Value = 0
            Form1.ProgressBar1.Visible = False
            value = 1
            Return value
            GoTo finFunction
        End Try
        ctx.Credentials = New SharePointOnlineCredentials(username, securedPassword)

        url = "https://name_company.sharepoint.com/sites/test/DATA%20BASE/DEVICES.xlsx"
        ctx = New ClientContext(url)
        ctx.Credentials = New SharePointOnlineCredentials(username, securedPassword)
        DownloadFile(url, ctx.Credentials, "C:\Users\Public\Documents\TEST.xlsx")
        Form1.ProgressBar1.Value = 3


        MsgBox("UPDATED COMPLETED", MsgBoxStyle.Information + MessageBoxButtons.OK, "ABB - MNS PRO")

        Form1.ProgressBar1.Visible = False
        Form1.Label115.Visible = False
finFunction:
    End Function

    Sub DownloadFile(ByVal webUrl As String, ByVal credentials As ICredentials, ByVal fileRelativeUrl As String)
        Using client = New WebClient()
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
            client.Headers.Add("User-Agent: Other")
            client.Credentials = credentials
            client.DownloadFile(webUrl, fileRelativeUrl)
        End Using
    End Sub

    Sub DeleteFilesInsideFolder(ByVal target_folder_path As String)
        ' loop through each file in the target directory
        For Each file_path As String In Directory.GetFiles(target_folder_path)
            ' delete the file if possible...otherwise skip it
            Try
                My.Computer.FileSystem.DeleteFile(file_path)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "TEST")
            End Try
        Next
    End Sub

    <DllImport("netapi32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Unicode)>
    Public Function NetUserChangePassword(
     <MarshalAs(UnmanagedType.LPWStr)> ByVal OldPass As String) As Integer
    End Function

    Public Sub ChangePassword(ByVal oldPassword As String)
        Try
            NetUserChangePassword(oldPassword)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    Sub ActivateOL()
        'Error 429 occurs with GetObject if Outlook is not running.
        On Error Resume Next
        Dim objOutlook = GetObject(, "Outlook.Application")

        If Err.Number = 429 Then 'Outlook is NOT running.
            MsgBox("Outlook is not running")
        End If
    End Sub
End Module

Thanks for the support.

First Update

    Public Function updateDb(value As Integer)
        Try

            ActivateOL()

            Dim outlook As Outlook.Application = Nothing

            Try
                outlook = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
            Catch
                outlook = New Outlook.Application()
            End Try
    End Sub

    Sub ActivateOL()
        'Error 429 occurs with GetObject if Outlook is not running.
        Dim objOutlook = GetObject(, "Outlook.Application")

        If Err.Number = 429 Then 'Outlook is NOT running.
            MsgBox("Outlook is not running")
        End If
    End Sub
vb.net
outlook
asked on Stack Overflow Jan 28, 2021 by LARC • edited Jan 29, 2021 by LARC

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0