For Next loop error: Option Strict On disallows late binding
For Next loop error: Option Strict On disallows late binding
我有以下产生错误的代码:
Option Strict On
Imports Microsoft.Office.Interop
Public Class Form1
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
xlWorkBook = xlApp.Workbooks.Open("C:\Book1.xlsx")
xlApp.Visible = True
For i = 1 To xlWorkBook.Worksheets.Count
xlWorkBook.Worksheets(i).Visible = True
Next
End Sub
End Class
谢谢。
我猜你要的是:
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
xlWorkBook = xlApp.Workbooks.Open("C:\Book1.xlsx")
xlApp.Visible = True
For i = 1 To xlWorkBook.Worksheets.Count
xlWorkSheet = CType(xlWorkBook.Worksheets(i), Excel.Worksheet)
xlWorkSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible
'If you don't want to make use of xlWorkSheet you can use this:
'CType(xlWorkBook.Sheets(i), Excel.Worksheet).Visible = Excel.XlSheetVisibility.xlSheetHidden
Next
End Sub
您应该始终关注对象的使用,因为这将提供对象属性的正确用法。注意 Visible = True
更改为 Visible = Excel.XlSheetVisibility.xlSheetVisible
我有以下产生错误的代码:
Option Strict On
Imports Microsoft.Office.Interop
Public Class Form1
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
xlWorkBook = xlApp.Workbooks.Open("C:\Book1.xlsx")
xlApp.Visible = True
For i = 1 To xlWorkBook.Worksheets.Count
xlWorkBook.Worksheets(i).Visible = True
Next
End Sub
End Class
谢谢。
我猜你要的是:
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
xlWorkBook = xlApp.Workbooks.Open("C:\Book1.xlsx")
xlApp.Visible = True
For i = 1 To xlWorkBook.Worksheets.Count
xlWorkSheet = CType(xlWorkBook.Worksheets(i), Excel.Worksheet)
xlWorkSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible
'If you don't want to make use of xlWorkSheet you can use this:
'CType(xlWorkBook.Sheets(i), Excel.Worksheet).Visible = Excel.XlSheetVisibility.xlSheetHidden
Next
End Sub
您应该始终关注对象的使用,因为这将提供对象属性的正确用法。注意 Visible = True
更改为 Visible = Excel.XlSheetVisibility.xlSheetVisible