保存并关闭visio文档visual basic macro

时间:2010-10-11 22:10:07

标签: excel-vba visio vba excel

我想创建一个visio页面,添加一些形状,用给定的文件名存储它并关闭它。

目前,对象/模板工具栏始终处于活动状态,因此存储在给定的文件名下。

存储当前图纸的最佳方法是什么? 感谢

Dim visioApp, visioPage as Object
Set visioApp = CreateObject("visio.application")

visioApp.Documents.AddEx ("")      
Set visioPage = visioApp.ActiveWindow.Page
Set visioStencil = visioApp.Documents.Add("BASFLO_M.VSS")

' add shapes

visioApp.ActiveDocument.SaveAs ("c:\.......vsd")
visioApp.ActiveDocument.Close

1 个答案:

答案 0 :(得分:2)

正如您所指出的,当您打开模板时,活动文档会发生变化。您可以将其更改回您正在编辑的文档,如下所示:

Set visioApp = CreateObject("visio.application")

visioApp.Documents.AddEx ("")
Set visioPage = visioApp.ActiveWindow.Page

' Remember which window is active '
Set visioWindow = visioApp.ActiveWindow

Set visioStencil = visioApp.Documents.Add("BASFLO_M.VSS")

' Reactivate the drawing window '
visioWindow.Activate

visioPage.Drop visioStencil.Masters(1), 4, 4

visioApp.ActiveDocument.SaveAs "c:\temp\mydoc.vsd"
visioApp.ActiveDocument.Close

您还可以使用对您创建的文档对象的引用,而不是依赖于活动文档:

Set visioApp = CreateObject("visio.application")

' Get a reference to the docment you are creating'
Set visioDoc = visioApp.Documents.AddEx("")
Set visioPage = visioApp.ActiveWindow.Page
Set visioStencil = visioApp.Documents.Add("BASFLO_M.VSS")

visioPage.Drop visioStencil.Masters(1), 4, 4

' Use the document object, not the active document '
visioDoc.SaveAs "c:\temp\mydoc1.vsd"
visioDoc.Close

我有最后一个建议。我建议您根据基本流程图模板创建新文档,而不是创建新文档然后创建模板。通过执行此操作,您可以创建一个文档,其中包含网格,字体等所有相同的默认设置,如果您在用户界面中选择了该模板,则会创建基本流程图。使用该模板的另一个好处是,每次重新打开您创建的文档时,将在文档的工作区中打开流程图模具。试试这个:

Set visioApp = CreateObject("visio.application")

' BASFLO_M.VST is the filename of the Basic Flowchart Template (metric) '
Set visioDoc = visioApp.Documents.Add("BASFLO_M.VST")
Set visioPage = visioApp.ActiveWindow.Page

' The stencil will be already open as part of the BASFLO_M.VST workspace '
Set visioStencil = visioApp.Documents("BASFLO_M.VSS")

visioPage.Drop visioStencil.Masters(1), 4, 5
visioPage.Drop visioStencil.Masters(1), 5, 4

visioDoc.SaveAs "c:\temp\mydoc2.vsd"
visioDoc.Close