列出任务栏中所有打开的WorkBook

时间:2015-11-16 11:20:46

标签: vb.net excel visual-studio-2013 import-from-excel

我试图在任务栏中列出所有打开的工作簿及其相应的工作表,之后我应该能够选择列表中的一个工作簿并打开它。我的第一个尝试是在任务管理器中显示Excel进程,但它只显示一个打开的工作簿而不检测工作表的数量。

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

    Dim name As Process() = Process.GetProcessesByName("Excel")
    For Each names In name
        ListView1.Items.Add(names.MainWindowTitle)
        If names.MainWindowTitle <> "" Then
            ListBox1.Items.Add(names.MainWindowTitle)
        End If
    Next
End Sub

接下来,我尝试使用此代码,但我只能显示一个打开的工作簿,我不知道如何循环它们。如果您有任何其他方法,我不会有更改整个代码的问题,因为我不确定它是否是解决方案。

    Dim oXL As Microsoft.Office.Interop.Excel.Application
    oXL = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)


    oXL.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized

    Dim y As String
    y = oXL.ActiveWorkbook.Name

           ListBox1.Items.Add(y)

1 个答案:

答案 0 :(得分:0)

  Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    Dim objWorkBook As Excel.Workbook = Nothing

    Dim totalWorkBooks As Integer = objExcel.Workbooks.Count

    For i As Integer = 1 To totalWorkBooks

        objWorkBook = objExcel.Workbooks(i)

        With objWorkBook
            FullName = .FullName
            OnlyName = .Name