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();
}