无法将“System .__ ComObject”类型的COM对象转换为Excel

时间:2018-01-08 14:36:23

标签: excel vb.net visual-studio-2015

我有一个简单的测试应用程序来演示,它必须是某种方式的环境,因为编译的应用程序适用于其他PC而不是我的!我已经完成的研究建议卸载VS2015,我已经完成但仍然没有工作。

这是代码

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim sheet As Excel.Worksheet

    xls.Workbooks.Add()
    book = xls.ActiveWorkbook
    sheet = book.ActiveSheet

    sheet.Cells(1, 1) = "Some Text"
    sheet.Cells(1, 2) = "More Text"

    xls.Workbooks.Close()
    xls.Quit()

    releaseObject(sheet)
    releaseObject(book)
    releaseObject(xls)

    MsgBox("Worked", MsgBoxStyle.Information)

  End Sub
  Private Sub releaseObject(obj As Object)
    Try
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
      obj = Nothing
    Catch ex As Exception
      obj = Nothing
      MessageBox.Show("Exception Occured while releasing object " & ex.ToString())
    Finally
      GC.Collect()
    End Try
  End Sub
End Class

它落在了... sheet.Cells(1,1)=“Some Text”

现在我已经卸载了VS2015,我正在使用上面编译的应用程序来证明我的机器(仍然失败)一旦找到正确的修复程序就可以正常工作。

但我正在努力找出下一步该做什么?如果可以避免的话,我真的不想擦机器。

0 个答案:

没有答案