MORE INFORMATION
Debug programs
You can debug Microsoft Office Word 2003 projects and Microsoft Office Excel 2003 projects by using the same Microsoft Visual Studio .NET tools that you use
for other projects. When you build your project in debug mode and then you run your project in debug mode, Visual Studio .NET starts Word or starts Excel and then attaches to the Word process or to the Excel process, respectively. Because your assembly is running in the Word process space or in the Excel process space, you can debug the assembly.
Handle errors
If you do not use exception handling in your Microsoft Office 2003 solution, errors (exceptions) that occur in your code are only displayed if you set the
Exceptions option to
Break into the debugger, as follows.
Note The following code sample assumes that you are developing a Microsoft Office Excel 2003 solution:
Microsoft Visual Basic .NETPrivate Sub ThisWorkbook_Open() Handles ThisWorkbook.Open
System.IO.File.Open("c:\nonexistant.file", System.IO.FileMode.Open)
System.Windows.Forms.MessageBox.Show("ThisWorkbook_Open")
End Sub
Microsoft Visual C# .NETprotected void ThisWorkbook_Open()
{
System.IO.File.Open("c:\nonexistant.file", System.IO.FileMode.Open);
System.Windows.Forms.MessageBox.Show("ThisWorkbook_Open");
}
When you run this code, no errors occur. However, the message box is not displayed. To display the errors in your code, put potentially problematic code in a
Try/Catch block, as follows:
Visual Basic .NETPrivate Sub ThisWorkbook_Open() Handles ThisWorkbook.Open
Try
System.IO.File.Open("c:\nonexistant.file", System.IO.FileMode.Open)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
System.Windows.Forms.MessageBox.Show("ThisWorkbook_Open")
End Sub
Visual C# .NETprotected void ThisWorkbook_Open()
{
try
{
System.IO.File.Open("c:\nonexistant.file", System.IO.FileMode.Open);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
System.Windows.Forms.MessageBox.Show("ThisWorkbook_Open");
}
When you run this code, the error messages are displayed and the status messages are displayed.
To break into the debugger when an exception occurs, follow these steps:
- On the Debug menu, click Exceptions.
- Under Exceptions, click Common Language Runtime Exceptions.
- Under When the exception is thrown, click Break into the debugger.
Note The default option is Continue.
If you select the
Break into the debugger option, all the exceptions break into the debugger, including the exceptions that you handled and the first-chance exceptions that runtime generated. Errors that refer to
msosec not being found appear in every project. For example, you may receive the following error message:
A first chance exception of type 'System.IO.FileNotFoundException'
occurred in WRDomainSetup. Additional information: File or assembly
name msosec, or one of its dependencies, was not found.
You can ignore these messages.