How to access an already opened Excel file in C#?

后端 未结 2 1392
借酒劲吻你
借酒劲吻你 2020-12-09 23:08

I have an excel workbook opened via double-clicking it in windows explorer but cannot access it in code

Excel.Application xlApp = (Application)Marshal.GetAct         


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 23:56

    If all your workbooks are opened in the same Excel instance (you can check this by checking if you can switch from one to the other using Alt-tab). You can simply refer to the other using Workbooks("[FileName]"). So, for example :

    Dim wb as Workbook //for C#, type Excel.Workbook wb = null;
    Set wb = Workbooks("MyDuperWorkbook.xlsx") //for C#, type wb = Excel.Workbooks["MyDuperWorkbook.xlsx"];
    wb.Sheets(1).Cells(1,1).Value = "Wahou!"
    

提交回复
热议问题