简体   繁体   中英

Excel Interop - Create visible workbook while keeping main hidden

Suppose I have the following set up in a WinForms application:

Dim ExcelApp As Microsoft.Office.Interop.Excel.Application
Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook
Dim wksDataVals As Microsoft.Office.Interop.Excel.Worksheet

ExcelApp.Visible = False

Basically, I have an invisible instance of Excel running in the background with a workbook open in it called ExcelWorkbook and a sheet the app is manipulating called wksDataVals . And the user is manipulating this sheet (and others) via the userform and seeing some outputs based upon those manipulations.

Now, on my form I have a button Show Excel Sheet where, once the user clicks it, I want to show them a copy of wksDataVals in a new, standalone, workbook while keeping the original hidden in the background for further data manipulation. I believe that I need to do this by creating a new instance of Excel since I want to keep this one hidden.

Is there a better way to do this than:

  1. Saving the worksheet
  2. Creating a new (visible) instance of Excel
  3. Re-opening the worksheet in that instance

(Which can create an issue with the worksheet now only being read-only since I want it to stay open in the other Excel instance) - I know I can overcome this by re-saving the worksheet as a new name AGAIN, but this all seems like overkill.

Any ideas about how I can accomplish this in the easiest way?? - Maybe there's a way to save the workbook / sheet internally and feed it to a new Excel instance rather than making Excel try and open a saved sheet??

Thanks!

As an answer to my own question in case anyone out there happens to have the same question come up for them (please, experts, if you have a better way, let me know), what I ended up doing was:

  1. Create a new instance of Excel
  2. Create a blank new workbook in that instance
  3. Copy the Range I cared about from the hidden sheet
  4. Pasted it (Values and formatting in my case was all I wanted) onto the new workbook
  5. Made the new instance of Excel visible
  6. Set the reference to the new app instance, workbook, sheet, etc = nothing, so now it was no longer connected to my program.

Seemed to do the trick wonderfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM