将形状添加到新的Visio文档

Dav*_*een 4 c# visio office-interop

我有这个代码创建一个新的Visio文档并添加一个矩形.它有效,但我不想打开另一个文档来获取Masters集合.问题是新文档有一个空的Masters形状集合.我在Document类中找不到一个方法来向Masters集合添加形状,并且我可以找到添加形状的所有示例,假设您有一个现有文档.有没有更好的方法来做我想要的?

// 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.";
Run Code Online (Sandbox Code Playgroud)

Joh*_*ith 5

你是对的 - Masters系列是ReadOnly.文档通常以空主集合开始.通过从模板文档中删除主人来填充该集合.

如果要使用预先填充的Masters集合创建新文档,则可以创建自己的模板(.vst),然后将新文档基于该文档.例如:

Visio.Document vDoc = vDocs.Add("MyTemplateFile.vst");
Run Code Online (Sandbox Code Playgroud)

通常,您将模板和模板打包在一起,然后始终通过从相应的模板文档(.vss)中删除主模板来创建形状.

Masters还有一个MatchByName属性.当此属性设置为true时,删除主服务器,Visio首先检查工程文档主服务器集合中是否存在相同的主服务器.如果它确实会丢弃该主服务器的实例.如果不是,将根据原始模板添加新的主模板.有关更多信息,请查看这两个链接:

如果您真的想在代码中创建自己的母版,可以在页面上绘制/删除自己的形状,然后使用Document.Drop方法将其添加到母版集合中.

此外,如果您想按名称使用master,那么在使用它之前,您需要遍历masters集合以检查它是否存在.

  • 查看这两个链接[分析流程之间的连接 - VisGuy.com](http://www.visguy.com/2009/04/22/analyze-connectivity-between-process-flows/)[以编程方式创建Visio流程图 - VisGuy.com](http://www.visguy.com/2006/09/13/create-visio-flowcharts-programmatically/) (2认同)