RE: C#: Excel unable to debug - code runs fine when sheet is simpl

Tech-Archive recommends: Fix windows errors by optimizing your registry



Found the solution at the post below. Problem is when both 1.1 and 2.0
frameworks are installed, have to explicitly tell debugger to use 1.1.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=304240&SiteID=1


"ExitMachine" wrote:

Sorry, forgot to mention - VS 2003, Excel 2003, XP SP2.

"ExitMachine" wrote:

Have a very simple C# Excel work*** project I've created to try and
diagnose. Class has one event handler setup for a CommandButton. Command
button simply puts some text into cell A1

Builds fine, when I open *** from the file system, click the button, text
is output - great.

Have a breakpoint in the button click event handler. Debug -> Start, Excel
opens very briefly (about 1 sec), closes, debugging ends. Cannot get the
breakpoint to hit.

After this, when I try to open the *** from the file system, get message
"The document 'TestDebug.xls' caused a serious error the last time it was
opened." and the doc gets set to disabled. Can get it re-enabled

Perhaps I am missing something, or maybe Excel VSTO projects don't support
debug stops at breakpoints. I've copied the code from my file below - have
removed comments and left out the "Generated initialization code" section.

Thanks very much.

---------------------------------------------------

using System;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using MSForms = Microsoft.Vbe.Interop.Forms;

// Office integration attribute. Identifies the startup class for the
workbook. Do not modify.
[assembly:System.ComponentModel.DescriptionAttribute("OfficeStartupClass,
Version=1.0, Class=ExcelTest.OfficeCodeBehind")]

namespace ExcelTest
{
public class OfficeCodeBehind
{
internal Excel.Application ThisApplication
{
get { return thisApplication;}
}

internal Excel.Workbook ThisWorkbook
{
get { return thisWorkbook;}
}

private Excel.Application thisApplication = null;
private Excel.Workbook thisWorkbook = null;

private Excel.WorkbookEvents_OpenEventHandler openEvent;
private Excel.WorkbookEvents_BeforeCloseEventHandler beforeCloseEvent;

protected void ThisWorkbook_Open()
{
MSForms.CommandButton testButton = (MSForms.CommandButton)
this.FindControl( "testButton" );
testButton.Click += new MSForms.CommandButtonEvents_ClickEventHandler(
testButton_Click );
}

protected void ThisWorkbook_BeforeClose(ref bool Cancel)
{
Cancel = false;
}

private void testButton_Click()
{
Excel.Work*** *** = (Excel.Work***)
ThisApplication.Sheets.get_Item( 1 );
Excel.Range rng = ***.get_Range ( "A1", "A1" );

rng.Value2 = "Test Debug";
}
}
}

.


Quantcast