VSTO: Visual Studio designer reusing running Excel instance instead of starting its own
When you create a VSTO Workbook project in Visual Studio, VS creates a new Excel (xlsx) file and allows you to edit directly from within Visual Studio. The Excel workbook appears in a new tab, alongside any code or forms:
To support this, Visual Studio needs to run an instance of EXCEL.EXE
at design time.
The problem is that unfortunately, instead of always running its own instance, if it finds one already running it will use that one. So if for some reason I had Excel already open on my PC, Visual Studio will reuse that instance.
This causes several problems: if Excel was running a long computation, it will freeze Visual Studio; if in VS I open a modal Excel window (e.g. cell or font properties), it freezes my existing Excel window (the one outside VS). If VS crashes, the Excel.exe still has a handle on the project's excel file, but I can't kill it because of my other open files in Excel. And finally, if I happened to have Excel 2003 open on the PC, and my project is using Excel 2007, VS will still attempt to use the 2003 instance currently running, which will then be confused at being asked to open a 2007 file, try to convert it, etc., which results in a complete mess.
The only solution I have found is to close ALL EXCEL.EXE
instances, only after run VS, open the VSTO project, VS will then find no running instances of excel and 开发者_JAVA百科run its own, and then I can re-open my previously opened files in new instances which I open myself. This is not always possible though and can be a real pain, so I would like to find a way of making VS open its own, personal new instance every time.
Have you tried changing the DEBUG options for the project in VS to point explicitly to Excel.exe and name the project XLSX file on the excel command line?
that's the only thing I can think of to try to always get a second instance of excel.
Office apps tend to want to use existing instances, so this behavior doesn't surprise me. Word can also be tricky.
And when you start talking about having multiple versions of Office loaded on a single dev machine (Which, BTW, I do as well), things can get really strange fast.
This simple C# console application can open a blank, invisible EXCEL.EXE process. You can schedule this program to run when logging in before any other Excel instance starts.
using System;
using System.Runtime.InteropServices;
//C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for
//Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
using Excel = Microsoft.Office.Interop.Excel;
class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
static void Main(string[] args)
{
Console.WriteLine("Opening new excel process...");
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
try
{
Excel.Application Ea = new Excel.Application();
Ea.Workbooks.Add();
Ea.Visible = false;
}
catch (Exception exep)
{
Console.WriteLine(exep.Message);
Console.ReadKey();
}
}
}
精彩评论