vb.net Visual Studio Express (Visual Basic) 中的 Excel.Application 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39795081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel.Application error in Visual Studio Express (Visual Basic)
提问by Code
Using Excel 2010, Visual Studio Express 2013.
使用 Excel 2010、Visual Studio Express 2013。
I have added the Reference Object Library in Visual Studio for Microsoft Excel 14.0
我在 Visual Studio 中为 Microsoft Excel 14.0 添加了参考对象库
Imports Microsoft.Office.Core
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
End Sub
End Class
However it gives me the following error:
但是它给了我以下错误:
Type 'Excel.Application' is not defined.
Type 'Excel.Workbook' is not defined.
Type 'Excel.Worksheet' is not defined.
Type 'Excel.Range' is not defined.
未定义类型“Excel.Application”。
未定义类型“Excel.Workbook”。
未定义类型“Excel.Worksheet”。
未定义类型“Excel.Range”。
If I am using a wrong reference library for the excel version, please show how I can go about adding the proper object library to the list.
如果我为 excel 版本使用了错误的参考库,请说明如何将正确的对象库添加到列表中。
回答by djv
You have imported the wrong namespace.
您导入了错误的命名空间。
Change
改变
Imports Microsoft.Office.Core
to
到
Imports Microsoft.Office.Interop
You will need to add a reference to Microsoft Excel 15.0 Object Libraryif you have not already (replace 15.0 with your version)
如果尚未添加对Microsoft Excel 15.0 对象库的引用,则需要添加(将 15.0 替换为您的版本)
And instead of late binding, you can use the correct types
而不是后期绑定,您可以使用正确的类型
' using the fully-qualified class name as an example.
' Excel.Application would work just fine because Microsoft.Office.Interop is imported
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = New Excel.Application()
' note that you have proper intellisense now because oXl is no longer just an Object.
oXl.Visible = True
Lastly, to properly clean up your Excel reference, since it's a COM object, put this in your code where you are finished with the objects (when closing the form for example). Do this for each COM object you create.
最后,为了正确清理您的 Excel 引用,因为它是一个 COM 对象,请将它放在您完成对象的代码中(例如关闭表单时)。为您创建的每个 COM 对象执行此操作。
System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL)
回答by Code
Figured it out. The previous code was from https://support.microsoft.com/en-us/kb/301982. However, when I edited the code in the following manner the errors went away.
弄清楚了。之前的代码来自https://support.microsoft.com/en-us/kb/301982。但是,当我按以下方式编辑代码时,错误消失了。
Imports Microsoft.Office.Core
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oXl As Object
Dim oWB As Object
'Dim oXL As Excel.Application
'Dim oWB As Excel.Workbook
'Dim oSheet As Excel.Worksheet
'Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
End Sub
End Class

