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
Anonymous
January 28, 2007
Bill, Thanks a lot for the fast response. It works for me.Anonymous
April 18, 2007
Bill, do you have a KB # for this problem?Anonymous
April 18, 2007
The comment has been removedAnonymous
May 06, 2007
I spent a little time today answering questions about some of the new features in Visio 2007, and noticedAnonymous
May 18, 2007
I have a Visio 2003 Automation Windows Forms project and met the same error. Bill's code solved the problem.Anonymous
May 24, 2007
good site. I find it from visiocafe. I met some trouble in doing the automation. It's lots of helpful for me.Anonymous
May 24, 2007
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.Anonymous
July 31, 2008
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, MartinAnonymous
October 23, 2015
its troubled me for a long time, thank you