我正在尝试调用打开的工作簿中的第 5 张工作表。当我从程序中打开工作簿时,我似乎能够做到:
Dim CurrentRun As New Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
Private Sub GeneralButtonOpener_Click(sender As Object, e As EventArgs) Handles GeneralButtonOpener.Click
CurrentRun.Visible = True
CurrentBook = CurrentRun.Workbooks.Add(MainTemplatePath)
CurrentSheet = CurrentBook.Worksheets(4)
CurrentSheet.Activate()
End Sub
但是如果文件已经打开,我所有尝试调用工作表的尝试都失败了:
Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
Dim CurrentBook As Microsoft.Office.Interop.Excel.Workbook
Dim CurrentSheet As Microsoft.Office.Interop.Excel.Worksheet
CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
CurrentBook = CurrentRun.Workbooks
CurrentSheet = CurrentBook.Sheets(4)
CurrentSheet.Activate()
或者
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
CurrentBook = CurrentRun.ActiveWorkbook
CurrentSheet = CurrentBook.Sheets(4)
CurrentSheet.Activate()
End Sub
我看过几个例子,但我不知道我哪里出错了。这让我感到惊讶,因为似乎有很多关于这个主题的问题。以太指向解决/解决此问题的位置或我特别做错了什么的指针将不胜感激。
谢谢!