BUG: You receive an "Unexpected error in Microsoft Visual Macros. It is safe to save your work and restart both Visual Studio Macros and Visual Studio" error message and Visual Studio macros fail when you close a modeless Windows form in Visual Studio (313519)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q313519

SYMPTOMS

Modeless Windows Forms that use the Show method to make the form visible may cause Visual Studio macros to fail.

Code such as the following causes the macros to fail:
Public Module Module1
    Sub MyTest()
        Dim f1 As Form
        f1 = New Form()
        f1.Width = 500
        f1.Height = 500
        f1.Show()
    End Sub
The form does not appear correctly and is unresponsive. When you close the form, you receive the End Program dialog box, which includes the End Task button. When you close the form, you may receive the following error message:
Unexpected error in Microsoft Visual Macros. It is safe to save your work and restart both Visual Studio Macros and Visual Studio

RESOLUTION

To fix this problem, add the following code:
While (<form>.Visible)
 Application.DoEvents()
End While
This code flushes the message queue so that the form can be closed correctly. The code should be inserted after the Show method. The complete code will be similar to the following:
Imports System.Diagnostics
Imports System.Drawing
Imports System.Windows.Forms

Public Module Module1
    Sub MyTest()
        Dim f1 As Form
        f1 = New Form()
        f1.Width = 500
        f1.Height = 500
        f1.Show()
       While (f1.Visible)
           Application.DoEvents()
       End While
    End Sub

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

After you experience the symptoms that are described in this article, the Visual Studio and Visual Studio Macro integrated development environments both appear to be fully functional. You can use them to modify code and save changes. However, subsequent attempts to run the macro that failed initially will fail also, and no error message will appear. If you unload the macro and attempt to reload it, the following error message appears:
Cannot load macro project 'your macro' for the following reason:
A project with the name 'your macro' already exists.
You must restart both the Visual Studio and the Visual Studio Macro integrated development environments you can run the macro.

Modification Type:MinorLast Reviewed:1/26/2006
Keywords:kbtshoot kbvs2005doesnotapply kbvs2005swept kbvs2002sp1sweep kbbug KB313519 kbAudDeveloper