vb.net& office:无法转换“Microsoft.Office.Interop.Excel.ApplicationClass”类型的COM对象

时间:2015-12-27 12:23:14

标签: vb.net excel

我正在开发一个vb.net应用程序,它应该打开并处理一个excel文件。 问题是在我的电脑中(并且在可以安装的其他电脑中也可能发生)我有Visio版本2013和Office(excel,word等等)版本2010.在vb中集成excel是如此简单,因为更多或者更少自动,只需几步即可完成..但同时也是如此愚蠢:调用interop.Excel函数,它解决了最新版本的办公室(2013),但我没有那个版本的excel! 我在网上搜索了很多关于这个问题的内容,并且每次我找到删除注册表项的解决方案,好吧,它工作正常,但每次我使用Visio时,似乎再次重新创建了注册表项。

我的问题是:

  • 我可以选择要将我的应用程序链接到哪个版本的Excel吗? 我可以理解修复版本我应该处理任何办公室版本 手动..但也许我更喜欢这样做..

  • 我可以在应用程序文件夹中复制一个dll,以确保该应用程序使用该DLL 版?我尝试使用Microsoft.Office.Interop.Excel.dll和Office.dll但仍然是问题

我不想按照以编程方式删除注册表项的方式,在我看来是不安全的.. 我无法相信办公室和.net之间的集成是如此愚蠢,以至于无法使用正确版本的excel安装!! ;-P

代码:

        Dim fileTest As String = FilePath

    Dim APP As New Application
    MsgBox(APP.Version)

    Dim oSheet As Excel.Worksheet = Nothing
    Dim workbook As Excel.Workbook = Nothing
    Dim IndexFound As Integer = 0
    Dim StartRow As Integer = 10
    Dim DateSelected As String = ""
    Dim resultOK As Boolean = False

    Try

        workbook = APP.Workbooks.Open(fileTest)

        oSheet = workbook.Worksheets("Activities")

        APP.DisplayAlerts = False

        oSheet.Range("A2").Value = "test"
        oSheet.Range("B3").Value = "Ciao"

    Catch ex As Exception
        MsgBox("Error: " + ex.Message)

        If Not resultOK Then
            If Not workbook Is Nothing Then
                workbook.Close()
                While System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook) > 0
                End While
                workbook = Nothing
            End If
            oSheet = Nothing
            workbook = Nothing
            APP.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(APP)
            APP = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End If
    End Try

End Sub

Reference excel interop Reference Office core

由于 安德烈

enter image description here

0 个答案:

没有答案