Logo
HOWTO: Debug an add-in for the VBA editor (32-bit or 64-bit) of Office with Visual Studio .NET.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Office 64-bit
Date: September 2012   Microsoft Office 32-bit
       
Introduction

This article explains how to debug an add-in for the 64-bit / 32-bit VBA editor of Office created with Visual Studio .NET and the .NET Framework 2.0, as explained in the article HOWTO: Create an add-in for the VBA editor (32-bit or 64-bit) of Office with Visual Studio .NET.

More information

The VBA editor of some Microsoft Office application is not an standalone executable (process), so to debug an add-in loaded in it, set a breakpoint in the OnConnection method in the add-in project of Visual Studio and follow one of the procedures below.

Note: the procedures will use Excel.exe and assume that you have used .NET Framework 2.0 in the add-in project of Visual Studio. You can adjust the procedures to higher .NET Framework versions (4.0, 4.5).

Procedure 1

  • Create a file named Excel.exe.config in the same folder than Excel.exe with the following content to force Excel to use .NET Framework 2.0 when loading managed add-ins:

    <?xml version ="1.0"?>
    <configuration>
       <startup>
          <supportedRuntime version="v2.0.50727" safemode="true"/>
          <requiredRuntime version="v2.0.50727" safemode="true"/>
       </startup>
    </configuration>
  • Open Microsoft Excel.
  • In Visual Studio, click the "Tools", "Attach to Process..." menu.
  • In the "Attach To" field, select the proper "Managed (version)" value:
    • For Visual Studio 2005 and 2008, select "Managed".
    • For Visual Studio 2010, select "Managed (v2.0, v1.1, v1.0)".
    • For Visual Studio 2012, select "Managed (v3.5, v3.0, v2.0)".
  • In the "Available Processes" list, select the Excel.exe process.
  • Click the "Attach" button.
  • In Excel, open the VBA editor and load the add-in. The breakpoint should be hit in the add-in project.

Procedure 2

  • Create a file named Excel.exe.config in the same folder than Excel.exe with the following content to force Excel to use .NET Framework 2.0 when loading managed add-ins:

    <?xml version ="1.0"?>
    <configuration>
       <startup>
          <supportedRuntime version="v2.0.50727" safemode="true"/>
          <requiredRuntime version="v2.0.50727" safemode="true"/>
       </startup>
    </configuration>
  • In the Project properties window of the add-in project, go to the "Debug" tab and in the "Start external program" enter the path to Excel.exe, for example "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE".
  • Click the "Debug", "Start Debugging" menu.
  • Excel will be launched.
  •  Open the VBA editor and load the add-in. The breakpoint should be hit in the add-in project.

Procedure 3

  • In the Solution Explorer of the Visual Studio project, right-click the solution node and click the "Add", "Existing Project..." menu entry.
  • Select the path to Excel.exe, for example "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE".
  • Right-click the EXCEL.EXE node and click the "Set as Startup Project" menu entry.
  • Right-click the EXCEL.EXE node and click the Properties menu entry.
  • In the Debugger Type field, change its value from "Auto" to "Managed (v2.0)".
  • Go to the "Tools", "Options..." window, "Projects and Solutions", "Build and Run" section, and uncheck the setting "Only build startup projects and dependencies on Run".
  • Click the "Debug", "Start Debugging" menu.
  • Excel will be launched.
  • Open the VBA editor and load the add-in. The breakpoint should be hit in the add-in project.

Related articles

Top