【问题标题】:Acces Outlook via Visual Basic error通过 Visual Basic 错误访问 Outlook
【发布时间】:2017-12-04 12:58:34
【问题描述】:

我正在尝试创建一个程序,该程序从 Outlook(2007 桌面版)收件箱中检索所有电子邮件并将它们放入 DataGridView。

代码:

Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt As DataTable
    Try
        Dim app As Outlook.Application = New Outlook.Application()
        Dim ns As Outlook.[NameSpace] = app.GetNamespace("MAPI")
        Dim inbox As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        ns.SendAndReceive(True)
        dt = New DataTable("Inbox")
        dt.Columns.Add("Subject", GetType(String))
        dt.Columns.Add("Sender", GetType(String))
        dt.Columns.Add("Body", GetType(String))
        dt.Columns.Add("Date", GetType(String))
        DataGridView1.DataSource = dt
        For Each item As Object In inbox.Items
            If TypeOf item Is Outlook.MailItem Then
                Dim item1 As Outlook.MailItem = CType(item, Outlook.MailItem)
                dt.Rows.Add(New Object() {item1.Subject, item1.Sender, item1.HTMLBody, item1.SentOn.ToLongDateString() & "" + item1.SentOn.ToLongTimeString()})
            End If
        Next
        MessageBox.Show("done")
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub
End Class

当我尝试构建项目时,出现以下错误:

System.Runtime.InteropServices.COMException (0x80040154):检索具有 CLSID {0006F03A-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败,原因是以下错误:80040154 未注册类(来自 HRESULT 的异常: 0x80040154 (REGDB_E_CLASSNOTREG))

更新

我已将 Compiler CPU 更改为 x86 和 x64,这并不能解决错误。

Target platform

【问题讨论】:

  • 你的代码有吗?因为我试图做这样的事情,但我找不到任何完美的解决方案......你从哪里得到这个?
  • 我不知道这段代码是否有效,因为错误导致我无法运行程序。

标签: vb.net winforms outlook clsid interopservices


【解决方案1】:

您的应用程序的目标平台是什么?它是基于 x86 的应用程序吗?

COM 类(在 32 位 COM dll 中)已注册的问题(可能),但由于应用程序在 64 位模式下运行,因此找不到正确的注册(32 位和 64 位 COM服务器是单独注册的)。

您还可以找到How to solve COM Exception Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))? page helpful.

【讨论】:

  • 我已经将VS2017的编译模式改为x64和x86,两种都试过了。仍然收到错误。
  • 尝试修复 MS Office。您的 Windows 注册表记录似乎有问题,它们已损坏。
  • 我还修复了 MS Office,但尚未删除并重新安装。你认为删除和重新安装会有帮助吗?
【解决方案2】:

导致错误的问题是两个应用程序(VS2015 和 MS Office 2007)没有在相同的权限下运行。打开具有相同权限(管理员或用户)的两个应用程序,该应用程序将运行。感谢您的帮助!

【讨论】:

    【解决方案3】:

    尝试此方法并向其发送参数,例如主题、正文、收件人地址、文件名...我相信这会有效..

    Private Sub SendfromOutlook(sSubject As String, sBody As String, sTo As String, sFilename As String)
        Dim oApp As Interop.Outlook._Application
        oApp = New Interop.Outlook.Application
        Dim oMsg As Interop.Outlook._MailItem
    
        Dim strS As String()
        strS = sTo.Split(",")
        For i As Integer = 0 To strS.Length - 1
    
            oMsg = oApp.CreateItem(Interop.Outlook.OlItemType.olMailItem)
            oMsg.Subject = sSubject
            oMsg.Body = sBody
            oMsg.To = strS(i)
    
            Dim str As String = sFilename
    
            If sFilename <> "" Then
    
    
                Dim sBodyLen As Integer = Int(sBody.Length)
                Dim oAttachs As Interop.Outlook.Attachments = oMsg.Attachments
                Dim oAttach As Interop.Outlook.Attachment
                oAttach = oAttachs.Add(str, , sBodyLen)
    
            End If
    
            oMsg.Send()
    
        Next
        MessageBox.Show("EMail Sent Successfully!", "Information", MessageBoxButtons.OK)
        ClearAll()
    End Sub
    

    在这里,我使用 To address with "," 来一次发送多封邮件..这就是为什么我只是拆分它并将这个函数用作循环函数( Dim strS As String() 和 strS = sTo.Split(","))

    【讨论】:

    • 不错的代码!但它没有回答我的问题。我的主要问题是我收到了错误,现在已修复。我在不同的权限(用户和管理员)上使用 Outlook 和 VS2015,当我在管理员上运行时,程序可以工作。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多