当 Option Strict On 时获取 Excel 个对象
Get Excel Object when Option Strict On
我从这个微软页面得到了以下代码。
https://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx
' Add Option Strict Off to the top of your program.
Option Strict Off
.
Private Sub getExcel()
Dim fileName As String = "c:\vb\test.xls"
If Not My.Computer.FileSystem.FileExists(fileName) Then
MsgBox(fileName & " does not exist")
Exit Sub
End If
' Set the object variable to refer to the file you want to use.
Dim excelObj As Object = GetObject(fileName)
' Show Excel through its Application property.
excelObj.Application.Visible = True
' Show the window containing the file.
Dim winCount As Integer = excelObj.Parent.Windows.Count()
excelObj.Parent.Windows(winCount).Visible = True
' Insert additional code to manipulate the test.xls file here.
' ...
excelObj = Nothing
End Sub
Option Strict Off 时一切正常
Option Strict On 时一切都不好
那么,如何解决 Option Strict On 时出现的错误?
小心!我想从同一个 excel 实例中获取特定的 excel 文件。
+1 努力使用 Option Strict On。 :)
但是,该代码使用的是所谓的后期绑定,这需要您将 Option Strict Off。但是,您可以通过创建新代码文件并使用 Partial Class 定义来包含需要后期绑定的代码,从而最大限度地减少 Option Strict Off 的范围。
您可以通过使用 VB CallByName function 和 Option Strict On 来获得,但这会很快变得丑陋而且可能非常慢。
还有一种使用本机 API 的高级技术,称为 COM 反射,应该与 Option Strict On 一起使用。此技术在文章中有所描述:[Basic Instincts - Inspecting COM Objects with Reflection].(https://msdn.microsoft.com/en-us/magazine/dd347981.aspx).
早期绑定(Option Strict On)的典型方法是添加对 Excel 主互操作程序集的引用。这种技术也有一些缺点,但它是迄今为止最简单的方法。
编辑:下面演示如何使用Excel PIA和早期绑定直接打开工作簿,类似于OP的原始代码。
Dim wbPath As String = "*** replace with path to your workbook ***"
Dim wb As Excel.Workbook = CType(GetObject(wbPath), Excel.Workbook)
'or
'Dim wb As Excel.Workbook = CType(Marshal.BindToMoniker(wbPath), Excel.Workbook)
Dim app As Excel.Application = wb.Application
app.Visible = True
wb.Windows(1).Visible = True
附带说明,如果您知道 Interop.Excel
早期绑定有效,则无需依赖它。您可以将错误通知更改为警告并仍然编译和 运行。
' Imports Microsoft.Office.Interop.Excel
Dim fileName = "c:\vb\test.xls"
If Not IO.File.Exists(fileName) Then MsgBox(fileName & " does not exist") : Exit Sub
Dim obj = GetObject(fileName, "Excel.Application")
Dim wb = TryCast(obj, Workbook)
wb.Application.Visible = True
wb.Windows(1).Visible = True
我从这个微软页面得到了以下代码。
https://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx
' Add Option Strict Off to the top of your program.
Option Strict Off
.
Private Sub getExcel()
Dim fileName As String = "c:\vb\test.xls"
If Not My.Computer.FileSystem.FileExists(fileName) Then
MsgBox(fileName & " does not exist")
Exit Sub
End If
' Set the object variable to refer to the file you want to use.
Dim excelObj As Object = GetObject(fileName)
' Show Excel through its Application property.
excelObj.Application.Visible = True
' Show the window containing the file.
Dim winCount As Integer = excelObj.Parent.Windows.Count()
excelObj.Parent.Windows(winCount).Visible = True
' Insert additional code to manipulate the test.xls file here.
' ...
excelObj = Nothing
End Sub
Option Strict Off 时一切正常
Option Strict On 时一切都不好
那么,如何解决 Option Strict On 时出现的错误?
小心!我想从同一个 excel 实例中获取特定的 excel 文件。
+1 努力使用 Option Strict On。 :)
但是,该代码使用的是所谓的后期绑定,这需要您将 Option Strict Off。但是,您可以通过创建新代码文件并使用 Partial Class 定义来包含需要后期绑定的代码,从而最大限度地减少 Option Strict Off 的范围。
您可以通过使用 VB CallByName function 和 Option Strict On 来获得,但这会很快变得丑陋而且可能非常慢。
还有一种使用本机 API 的高级技术,称为 COM 反射,应该与 Option Strict On 一起使用。此技术在文章中有所描述:[Basic Instincts - Inspecting COM Objects with Reflection].(https://msdn.microsoft.com/en-us/magazine/dd347981.aspx).
早期绑定(Option Strict On)的典型方法是添加对 Excel 主互操作程序集的引用。这种技术也有一些缺点,但它是迄今为止最简单的方法。
编辑:下面演示如何使用Excel PIA和早期绑定直接打开工作簿,类似于OP的原始代码。
Dim wbPath As String = "*** replace with path to your workbook ***"
Dim wb As Excel.Workbook = CType(GetObject(wbPath), Excel.Workbook)
'or
'Dim wb As Excel.Workbook = CType(Marshal.BindToMoniker(wbPath), Excel.Workbook)
Dim app As Excel.Application = wb.Application
app.Visible = True
wb.Windows(1).Visible = True
附带说明,如果您知道 Interop.Excel
早期绑定有效,则无需依赖它。您可以将错误通知更改为警告并仍然编译和 运行。
' Imports Microsoft.Office.Interop.Excel
Dim fileName = "c:\vb\test.xls"
If Not IO.File.Exists(fileName) Then MsgBox(fileName & " does not exist") : Exit Sub
Dim obj = GetObject(fileName, "Excel.Application")
Dim wb = TryCast(obj, Workbook)
wb.Application.Visible = True
wb.Windows(1).Visible = True