Handle errors and debug programs by using Microsoft Visual Studio Tools for the Microsoft Office System (824011)



The information in this article applies to:

  • Microsoft Visual Studio Tools for the Microsoft Office System version 2003

SUMMARY

Because of how managed code interacts with unmanaged code, the Microsoft Visual Studio .NET debugger does not display errors that Microsoft Office 2003 applications throw by default. This article discusses how to use Microsoft Visual Studio Tools for the Microsoft Office System to handle errors and to debug programs.

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 .NET
Private 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# .NET
protected 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 .NET
Private 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# .NET
protected 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:
  1. On the Debug menu, click Exceptions.
  2. Under Exceptions, click Common Language Runtime Exceptions.
  3. 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.

REFERENCES

For more information, visit the following Microsoft Web sites:

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbinfo KB824011 kbAudDeveloper