VSTO: how to get Visual Studio to start a new instance of Excel

Visual Studio needs to run an instance EXCEL.EXEto enable editing Excel worksheets in design mode from Visual Studio. Unfortunately, instead of always working independently, if he finds one already running, he will use it. If this instance worked, or, for example, runs a macro, the macro sometimes fails. And if for some reason I need to kill him (which I often do), he will obviously also stop working in VS.

The solution is to close all instances EXCEL.EXE, start VS, open the VSTO project, VS will not find excel instances running and will not execute it, and then I can reopen my previous instances. This is not always possible, so I would like to find a way to get VS to open a new instance every time.

+3
source share
1 answer

I am a VB guy, so my code may be missing (although it is just copied from here ).

First you want to declare a variable to hold a new instance of Excel:

Excel.Application oXL;

Then you need to set the variable to a new instance and make it visible:

oXL = new Excel.Application();
oXL.Visible = true;

Then only reference this instance in your code, for example,

oXL.Workbooks.Open ...

-1
source

Source: https://habr.com/ru/post/1784376/


All Articles