Buy Me a Coffee

Buy Me a Coffee!

Saturday, May 26, 2012

WPF Browser Application–Visio Automation 1

Opening existing Visio documents is fun and all, but we need to go a little farther to really start having fun.  Let’s start with the basic setup from the first article.  Build a project, add your controls and references, and then fill in the Page_Loaded method with new code.

1)  Start with creating a new blank document by calling the Add method:

visioControl.Document.Pages.Document.Application.Documents.Add("");

2)  Get a reference to your new document:


Visio.Documents visioDocs =
visioControl.Document.Pages.Document.Application.Documents;

3)  Open up your stencil library (I am using a custom, remote one, it works similarly with standard, local ones):


Visio.Document visioStencil =
visioDocs.OpenEx(@"http://smithmier.com/Visio/NewCake.vss",
(short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);

4)  Get a reference to the currently active page:


Visio.Page visioPage =
visioControl.Document.Pages.Document.Application.ActivePage;


5)  Choose your shape from the Stencil:


Visio.Master visioShapeMaster = visioStencil.Masters.get_ItemU(@"Cake");

6)  Drop the shape on the page, and get a reference to the new instance:


Visio.Shape visioShape = visioPage.Drop(visioShapeMaster, 4.25, 5.5);

7)  Then finally we add some text to the shape to make it interesting:


visioShape.Text = @"Cake";

Source:  VisioWPF2.zip