Adding Shapes to a New Visio Document
Asked Answered
P

2

4

I have this code that creates a new Visio document and adds a rectangle. It works, but I don't like having to open another document to get the Masters collection from it. The issue is the new document has an empty Masters shape collection. I couldn't find a method in the Document class to add shapes to the Masters collection and all the examples I could find for adding shapes assumed you had an existing document. Is there a better way to do what I want?

// create the new application
Visio.Application va = new Microsoft.Office.Interop.Visio.Application();

        // add a document
        va.Documents.Add(@"");

       // Visio.Documents vdocs = va.Documents;

        // we need this document to get its Masters shapes collection
        // since our new document has none 
        Visio.Document vu = vdocs.OpenEx(@"C:\Program Files (x86)\Microsoft       Office\Office12\1033\Basic_U.vss", (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);

        // set the working  document to our new document
        Visio.Document vd = va.ActiveDocument;

        // set the working page to the active page
        Microsoft.Office.Interop.Visio.Page vp = va.ActivePage;

      // if we try this from the Masters collection from our new document
      // we get a run time since our masters collection is empty
     Visio.Master vm  = vu.Masters.get_ItemU(@"Rectangle");
    Visio.Shape visioRectShape = vp.Drop(vm, 4.25, 5.5);
        visioRectShape.Text = @"Rectangle text.";
Posticous answered 27/2, 2012 at 17:53 Comment(0)
A
5

You're right - the Masters collection is ReadOnly. Documents normally start off with an empty masters collection. The collection gets populated by dropping masters from a stencil document.

If you want to create a new document with a pre-populated Masters collection then you could create your own template (.vst) and then base your new document on that. For example:

Visio.Document vDoc = vDocs.Add("MyTemplateFile.vst");

Normally you would package your stencils and templates together and then always create shapes by dropping a master from the respective stencil document (.vss).

Masters also have a MatchByName property. Dropping a master when this property is set to true, Visio first checks that a master of the same exists in the drawing document masters collection. If it does an instance of that master will be dropped. If not a new master will be added based on the original stencil. Have a look at these two links for more information:

If you really want to create your own masters in code, you can draw / drop your own shapes on the page and then use the Document.Drop method to add it to the masters collection.

Also if you want to use a master by name then you'll need to loop through the masters collection to check that it exists before you use it.

Albarran answered 28/2, 2012 at 9:50 Comment(5)
thanks. Do you know of a decent forum and/or some good resources for generating Visio documents programattically? I spent a few hours on this yesterday and mostly fumbled my way through. I still have some questions such as how do I figure out what connection points a shape has and how can I address a specific connection point?Posticous
Check out these two links Analyze Connectivity Between Process Flows - VisGuy.com Create Visio Flowcharts Programmatically - VisGuy.comAlbarran
Also, in no particular order: Visio Automation - Saveen Reddy on CodePlex Visio Guy forum TechNet Developing Visio Solutions - old but a great resource Visio 2010 SDK and for a comprehensive list, I'd have a look at the links section on VisGuy.comAlbarran
thanks for the links, they have been helpful. The VisioAutomation looks interesting but I am having trouble finding the docs. I have sent a message to Saveen letting him know the link isn't working.Posticous
Just an update to the VisioAutomation link above that moved a while ago from CodePlex to GitHub: github.com/saveenr/VisioAutomationAlbarran
H
2

I think you will find this on-line book extremely useful : http://msdn.microsoft.com/en-us/library/aa245244(v=office.10).aspx

Hermilahermina answered 29/2, 2012 at 15:29 Comment(1)
thanks. I have bee going through that - I found it this week. It has been helpful.Posticous

© 2022 - 2024 — McMap. All rights reserved.