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

Tuesday, May 8, 2012

WPF Browser Application–Visio Viewer

I decided to follow up my previous article on Embedding Visio in a WPF Browser Application by taking a look at what can be done with the Visio Viewer.  Let’s start out like we did before with a WPF Browser Application:
--New Project Image

and add your reference to the WindowsFormsIntegration part:
--Add Reference Image
and open up a Visual Studio Command Prompt (2010) and make your Interop assembly, this time from the VVIEWER.DLL (I found mine in C:\Program Files (x86)\Microsoft Office\Office14) using the command:
aximp VVIEWER.DLL
This time, two dlls are created: AxVisioViewer.dll and VisioViewer.dll.  We are only going to be using AxVisioViewer.dll at this time, so add your reference:
--Add Reference Image
You then drop your WindowsFormsHost on the panel or add the following in the xaml:
<WindowsFormsHost HorizontalAlignment="Stretch" Name="windowsFormsHost1" VerticalAlignment="Stretch" />

Add a line to your class for an instance of the AxViewer:

private AxViewer visioControl = new AxViewer();

and then update the constructor for the page to add the control to the WindowsFormsHost:


this.windowsFormsHost1.Child = this.visioControl;


add an event handler for the page loaded event and drop in a line to load your Visio document:

this.visioControl.SRC = @"http://smithmier.com/Visio/TestDrawing.vsd";

and finally, update your project security to a Full Trust Application on the solution properties screen:

--FullTrust

Notice the difference?  Go ahead, look back.  I will wait.

Find them?  AxDrawingControl becomes AxViewer and .Src becomes .SRC.  That is all.

Of course, there are many other things that changed, but in the very simple example code it isn’t apparent.  The two main differences for end users is that they won’t need Visio installed to view the document with this version, and they won’t be able to alter the document in this version.  I will go through the changes in the object model and control available in future posts.

Source code: VisioViewerWPF.zip

Thursday, May 3, 2012

WPF Browser Application

Why would you want to do a WPF application in a browser?  Isn’t that what Silverlight is for, letting you do XAML on the Web?  But what if you want to embed something sinister like Visio in a web page?  Will Silverlight let you do that?  No!  At least I don’t think so, If you know how to do it, please let me know, but for the purposes of this blog post, please play along.  Now, to get started, let’s open up a new Visual Studio WPF Browser Application:

First we need to add a reference to the WindowsFormsIntegration part:

Next, we need to get an Interop assembly for the Visio OCX control (AxInterop.Microsoft.Office.Interop.VisOcx).  The prevailing wisdom on the web is to open up a WinForm project and drop a Visio control on the surface to have Visual Studio automatically generate the DLL for us.  I never really like using a hammer to put in screws, so I dug around a little bit and found that aximp is the tool that Microsoft intends you to use for this little task.  So, find your copy of VISOCX.DLL (mine was in C:\Program Files (x86)\Microsoft Office\Office14) and run the following command:
aximp VISOCX.DLL
to create your very own interop assembly.  The one creates is named AxVisOcx.dll by default, but it has the same content as the one generated by Visual Studio.  Add a reference to it in your Visual Studio project by using the Browse tab:

You then drop your WindowsFormsHost on the panel or add the following in the xaml:
<WindowsFormsHost HorizontalAlignment="Stretch" Name="windowsFormsHost1" VerticalAlignment="Stretch" />

Add a line to your class for an instance of the AxDrawingControl:

private AxDrawingControl visioControl = new AxDrawingControl();

and then update the constructor for the page to add the control to the WindowsFormsHost:

this.windowsFormsHost1.Child = this.visioControl;

add an event handler for the page loaded event and drop in a line to load your Visio document:

this.visioControl.Src = @"http://smithmier.com/Visio/TestDrawing.vsd";

and finally, update your project security to a Full Trust Application on the solution properties screen:



Source code: VisioWPF.zip