Sunday, September 23, 2012

Closing a document and loading another

One thing that I never found the right set of keywords to bring up in a set of search results is closing a loaded document and opening another within an embedded Visio control.  I am not sure if this is the correct way, or just a hack, but here is how I was able to achieve the results I wanted.

The method I ended up using was simplicity itself, once I figured it out.  Dispose of the control and create and bind a new one.  Simple right?  Here is the full code for the class:

public partial class MainWindow : Window
{
private AxDrawingControl visioControl;
private Visio.Document visioStencil;
private Visio.Documents visioDocs;
const string documentOne = @"c:\temp\TestDrawing.vsd";
const string documentTwo = @"c:\temp\TestDrawing2.vsd";
const string stencilOne = @"c:\temp\Cake.vss";

public MainWindow()
{
InitializeComponent();
SetupVisioControl();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadDocument(documentOne);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
visioControl.Dispose();
SetupVisioControl();
LoadDocument(documentTwo);
}

private void SetupVisioControl()
{
visioControl = new AxDrawingControl();
this.windowsFormsHost1.Child = this.visioControl;
}

private void LoadDocument(string document)
{
visioControl.Src = document;
visioDocs =
visioControl.Document.Pages.Document.Application.Documents;
visioStencil =
visioDocs.OpenEx(stencilOne, (short)Visio.VisOpenSaveArgs.visOpenDocked);
}
}

When I tried to simply use the close method on the individual documents as follows:


foreach (Visio.Document document in visioDocs)
{
document.Saved = true;
document.Close();
}

I consistently received a ComException on the document.close() call: This operation cannot be performed while doing in-place editing.

No comments:

Post a Comment