首页 > 解决方案 > 无法将 microsoft.office.interop.outlook.applicationclass 类型的 COM 对象转换为接口类型 Microsoft.Office.Interop.Outlook._Application

问题描述

在需要获取在 Windows 的 Outlook 会话中注册的邮件时执行开发。从这个意义上说,用户唯一需要做的就是输入他们的 Windows 会话密码才能登录,然后转到 SharePoint 并下载几个文件。在执行编程时,它向我发送以下错误。

请验证是否支持下一个错误:无法将 COM 对象类型“Microsoft.Office.Interop.Outlook.ApplicationClass”转换为接口类型“Microsoft.Office.Interop.Outlook._Application”。此操作失败,因为对 ID 为“{00063001-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:加载类型库/DLL 时出错。(来自 HRESULT 的异常:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。

我已经签入了 regedit,但子键为 0,我在其他网站上对此进行了一些调查,但我仍然遇到同样的问题。你能帮我看看错误在哪里吗?这是我开发的代码。

Outlook 版本为 2016 (O365) 和 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

感谢您的支持。

第一次更新

    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.netoutlook

解决方案


推荐阅读