Search code examples
vb.netexcelvisual-studio-2013import-from-excel

List all opened WorkBooks in the Task Bar


I am trying to list all the opened workbooks and their corresponding sheets in the task bar after that I should be able to select one workbook of the list and open it. My first try was to to show Excel process in Task-Manager but it only shows one open workbook without detecting the number of sheets.

  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

Next I tried to use this code but also I can only display one opened workbook, I don't know how to loop through them. I don't have problem of changing the whole code if you have any other method cause I am not sure that it's the solution.

    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)

Solution

  •   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