Visio 2007: Bug in Drawing Control


Please be aware that there is a bug in the Visio 2007 Drawing Control such that if you have the SRC property set and add a new page (either through the UI or through code), Visio crashes. Obviously this is really bad and we are actively working to get a fix, but in the meantime we’d like to provide a workaround. If you see the following error messages when adding a page in the control, you are possibly hitting this bug:

  • System.AccessViolationException was unhandled
    Message=”Attempted to read or write protected memory. This is often an
    indication that other memory is corrupt.”
  • ‘System.Runtime.InteropServices.SEHException’ occurred in system.windows.forms.dll
    Additional information: External component has thrown an exception.

We’ve provided code below for most of the major languages we support. I’ve highlighted in red in each case the actual fix, but we wanted to provide a little bit of the context of the code since you need to make sure to call the SendMessage fix after you have set the SRC property of the control.

VBA

‘ declare win32 function

Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

 

Private Sub LoadDocument()

‘ assign source file

DrawingControl1.Src = “C:\Drawing1.vsd”

‘ send private message to Visio to invoke workaround

SendMessage DrawingControl1.Window.WindowHandle32, 2100, 0, 0

‘ add page

DrawingControl1.Document.Pages.Add

End Sub

 

VB6

Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

 

Private Sub LoadDocument()

‘assign source file

DrawingControl1.Src = “C:\Drawing1.vsd”

‘send private message to Visio to invoke workaround

SendMessage DrawingControl1.Window.WindowHandle32, 2100, 0, 0

‘add page

DrawingControl1.Document.Pages.Add

End Sub

 

 

VB.NET

(need to Import System and System.Runtime.InteropServices)

‘Declare win32 function

<DllImport(“User32.dll”)>

Private Shared Function SendMessage(ByVal Handle As Int32, _

ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

End Function

 

Private Sub LoadDocument()

AxDrawingControl1.Src = “c:\\Drawing1.vsd”

SendMessage(AxDrawingControl1.Window.WindowHandle32, 2100, 0, 0)

AxDrawingControl1.Document.Pages.Add()

End Sub

 

 

C#

// declare win32 function

[DllImport(“user32.dll”, EntryPoint=”SendMessageA”)]

public static extern int SendMessage(int windowHandle, uint message, int wparam, int lparam);

 

private void LoadDocument ()

{

// assign source file

axDrawingControl1.Src = “C:\\Drawing1.vsd”;

// send private message to Visio to invoke workaround

SendMessage(axDrawingControl1.Window.WindowHandle32, // drawing window

2100, // private Visio message

0,

0);

// add page

axDrawingControl1.Document.Pages.Add();

}

 

 

C++

void LoadDocument()

{

// assign source file

drawingControl->Src(“C:\\Drawing1.vsd”);

 

// send private message to Visio to invoke workaround

::SendMessage( (HWND) drawingControl->GetWindow()->GetWindowHandle32(), // drawing window

2100, // private Visio message

0,

0);

// add page

drawingControl->GetDocument()->GetPages()->Add();

}


Comments (9)

  1. Christian says:

    Bill, Thanks a lot for the fast response. It works for me.

  2. LarryOsterman says:

    Bill, do you have a KB # for this problem?

  3. wmorein says:

    We have a KB in the pipe, but it is not released yet. I’ll reply again when it is available.

  4. I spent a little time today answering questions about some of the new features in Visio 2007, and noticed

  5. Yangyong says:

    I have a Visio 2003 Automation Windows Forms project and met the same error. Bill’s code solved the problem.

  6. flowchart says:

    good site. I find it from visiocafe. I met some trouble in doing the automation. It’s lots of helpful for me.

  7. flowchart says:

    I forget to say, it’s difficult to hide the toolbar in the visio with the vb language. I try the olecommand. but failed every time.

  8. Hi Bill,

    Do you know if a fix for this was ever found.  I have tried installing Visio SP1 but the problem still occurs.

    Thanks,

    Martin

  9. Wave.Yang says:

    its troubled me for a long time, thank you