我有这段代码,它创建一个新的Visio文档并添加一个矩形。它可以工作,但我不喜欢必须打开另一个文档才能从中获取Master集合。问题是新文档有一个空的主控形状集合。我在document类中找不到将形状添加到Master集合的方法,我能找到的所有添加形状的示例都假定您已经有了一个文档。有没有更好的方法来做我想做的事?
// 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.";发布于 2012-02-28 17:50:12
你说得对--大师的收藏是ReadOnly。文档通常从一个空的master集合开始。通过从模具文档中删除母版来填充该集合。
如果您想使用预先填充的Master集合创建一个新文档,那么您可以创建自己的模板(.vst),然后在此基础上创建新文档。例如:
Visio.Document vDoc = vDocs.Add("MyTemplateFile.vst");通常,您会将模具和模板打包在一起,然后始终通过从相应的模具文档(.vss)中拖出母版来创建形状。
Master也有一个MatchByName属性。删除母版此属性设置为true时,Visio首先检查绘图文档母版集合中是否存在相同的母版。如果是这样的话,该主服务器的一个实例将被删除。如果不是,将基于原始模板添加新的母版。有关更多信息,请查看这两个链接:
如果确实希望在代码中创建自己的母版,可以在页面上绘制/放置自己的形状,然后使用Document.Drop方法将其添加到母版集合中。
此外,如果您想按名称使用master,那么在使用它之前,您需要遍历master集合以检查它是否存在。
发布于 2012-02-29 23:29:47
我想你会发现这本在线书籍非常有用:http://msdn.microsoft.com/en-us/library/aa245244(v=office.10).aspx
https://stackoverflow.com/questions/9469739
复制相似问题