squillman
squillman

Reputation: 13641

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

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:

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.

Upvotes: 0

Views: 770

Answers (3)

jreichert
jreichert

Reputation: 1566

This works for me:

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

  ...
}

Upvotes: 1

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

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.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

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.

Upvotes: 0

Related Questions