Debug Excel VSTO add-in when launched by double-clicking existing file

740 views Asked by At

I have a bug in an Excel VSTO 4.0 C# add-in that I've developed but it is only occurring when double-clicking on an existing file to launch Excel. I'm trying to figure out a way to start the VS2013 debugger so that I can put in some breakpoints but none of the ways I've tried to start the debugger mimic the process of launching Excel by a double-click on a file.

Ways I've tried so far:

  • Launching Excel itself and then attaching to the process
  • Double-clicking an Excel file and then attaching to the resulting process
  • In the VS project Debug properties under Start Action, entering the path to Excel Excel.exe in the Start external program box and the path to an existing Excel file in the command line arguments box
  • Adding Excel.exe as a new existing project to my VS solution with the path to an existing file as an argument in the project properties, the setting it as the startup project for the solution.

None of these methods for starting the debugger are reproducing the bug. Is there another way to start debugging when double-clicking a file to launch Excel?

For what it's worth, the bug I'm experiencing is that an empty workbook will be created when double-clicking an existing file when Excel is not already running. I need to find out where / why that empty file is getting created. It doesn't happen if Excel is already running and you double-click a file to open it.

3

There are 3 answers

3
Eugene Astafiev On BEST ANSWER

Try to use any logging mechanisms to log any action in the code. Thus, you will be able to find what is going on in the code at runtime. For example, you can write actions to the log file. You may find the log4net library helpful.

4
Sarvesh Mishra On

I am assuming that you are puuting breakpoint in a method which handles the WorkbookOpen event.

When you double click an Excel file, it gets loaded and event WorkbookOpen is raised. VSTO add-in is not yet loaded so you can't get that event. After loading the workbook, Excel loads VSTO add-in.

So when your add-in is loaded, your handler for AddinStartup comes in action. In vb.net, the handler line appears as...

Private Sub ThisAddIn_Startup() Handles Me.Startup

You can now check for loaded workbook in ThisAddIn_Startup

If Application.ActiveWorkbook IsNot Nothing Then
    DoSomething(Application.ActiveWorkbook)
End If

The other way to mimic some of these debugging is to open file and close file, loading and unloading your addin using excel options manually.

  1. Put a breakpoint on Addin_Startup.

  2. Start normal debugging of VSTO Project.

  3. Load File.

  4. Go in excel options and unload your addin by unchecking it.

  5. Go in excel options and LOAD your addin by checking it.

0
jreichert On

This works for me:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
  System.Diagnostics.Debugger.Launch();

  ...
}