2012-02-27 33 views
4

我有這個代碼可以創建一個新的Visio文檔並添加一個矩形。它的工作原理,但我不喜歡打開另一個文件從它獲得碩士學位。問題是新文檔有一個空的Masters形狀集合。我無法在Document類中找到將形狀添加到Masters集合中的方法,並且我可以找到用於添加形狀的所有示例(假定您擁有現有文檔)。有沒有更好的方式去做我想要的?將形狀添加到新的Visio文檔

// 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."; 

回答

5

你是對的 - 大師系列是隻讀。文件通常以空主人集合開始。集合通過從模具文檔中刪除主人來填充。

如果您想創建一個帶有預填充Masters集合的新文檔,那麼您可以創建自己的模板(.vst),然後根據該文檔創建新文檔。例如:

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

通常你會打包模具和模板在一起,然後總是從各自的模板文件(.VSS)丟棄主創造形狀。

大師還具有MatchByName屬性。如果在將此屬性設置爲true時丟棄主設備,Visio將首先檢查繪圖文檔主集合中是否存在主設備。如果它確實會拋棄該主人的一個實例。如果沒有新的主人將根據原始模具添加。看看這兩個鏈接,瞭解更多信息:

如果你真的想在代碼中創建自己的主人,你可以畫/砸在自己的形狀頁面,然後使用Document.Drop方法將其添加到主集合中。

此外,如果你想通過名字使用主人,那麼你需要遍歷master集合來檢查它在你使用它之前是否存在。

+0

謝謝。你知道一個體面的論壇和/或用於生成Visio文檔的一些好的資源嗎?我昨天花了幾個小時,大部分時間都在摸索着。我仍然有一些問題,比如如何確定形狀的連接點,以及如何解決特定的連接點? – 2012-02-28 13:27:59

+2

查看這兩個鏈接 [分析過程流之間的連接 - 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/) – JohnGoldsmith 2012-02-28 14:17:41

+1

此外,沒有特定的順序: [Visio Automation - Saveen Reddy on CodePlex](http://visioautomation.codeplex.com/) [Visio Guy forum](http://visguy.com/vgforum/index.php) [TechNet](http://social.technet.microsoft。 com/Forums/en-AU/visiogeneral/threads) [開發Visio解決方案 - 舊的但很棒的資源](http://msdn.microsoft.com/zh-cn/library/aa245244%28office.10%29.aspx ) [Visio 2010 SDK]( http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=12365) 和一個全面的列表,我會看看[VisGuy.com上的鏈接部分](http ://www.visguy。com/visio-links /) – JohnGoldsmith 2012-02-28 14:18:19