Interacting with Excel from VB Windows Form application RRS feed

  • Question

  • Hi,

    I am new to VB and interacting with Office Applications (but not programming). I am working on a standalone VB application that I want to be able to open and read some data from an Excel spreadsheet.

    I found some code for launching the Excel application and opening a particular file (for which I have the path).

    That code basically goes like this:

        Imports Excel = Microsoft.Office.Interop.Excel

        Dim excelFilePath As String = "C:\Users\myUserName\some\arbitrary\file\path\mySpreadsheetName.xlsx"

        Dim excelApp As Excel.Application

        excelApp = New Excel.Application
        excelApp.Visible = True

    Dim workbook As Excel.Workbook = excelApp.Workbooks.Open(excelFilePath)

        Dim workbook As Excel.Workbook = excelApp.Workbooks.Open(excelFilePath)

    That works fine the first time through (and I'm excited to be able to write code that can read data out of that spreadsheet) but what I really need to do is first figure out whether Excel has already been launched.

    If it has (either by the user themselves, outside of this program, or by my program), then I just want to interact with the already-running instance and find out if it already has the Workbook I'm interested in (and direct it to open that workbook if it doesn't have it). (I think I've read enough APi docs to see how to navigate Workbooks and figure that part out.)

    If Excel is not already launched, then the code above can get it launched and open my workbook.

    Obviously, I need something to interact with prior to calling: New Excel.Application.

    That's got to be something above the Microsoft.Office.Interop.Excel API, and so is outside of the scope of the books I have.

    I have tried to dig around in here some, but I am a bit overwhelmed and not readily finding what I am imagining:

    Maybe someone could provide a simple program that either returns a reference to the running Microsoft.Office.Interop.Excel instance or simply prints "Excel is not running." ?


    Friday, December 20, 2019 8:31 PM