vba 保存和关闭 visio 文档 Visual Basic 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3910309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:10:41  来源:igfitidea点击:

save and close visio documents visual basic macro

excel-vbavisiovbaexcel

提问by user326667

I want to create a visio page, add some shapes, store it with a given filename and close it.

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

Currently, always the object/template toolbar is active and thus stored under the given filename.

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

What is the best way to store the current drawing? thanks

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

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

采纳答案by user326667

As you point out, when you open the stencil the active document changes. You can change it back to the document you are editing like this:

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

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

You could also use a reference to the document object you created and not rely on the active document:

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

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

I have one last suggestion. Instead of creating a new document and then a stencil I suggest you create a new document based on the Basic Flowchart template. By doing this you create a document with all the same default settings for grid, fonts, etc as the Basic Flowchart you would create if you selected that template in the user interface. Another benefit of using the template is that the flowchart stencils will be opened in the document's workspace every time the document you create is reopened. Try this:

我还有最后一个建议。我建议您不要创建一个新文档然后创建一个模板,而是基于基本流程图模板创建一个新文档。通过这样做,您可以创建一个文档,其网格、字体等的所有默认设置与您在用户界面中选择该模板时将创建的基本流程图相同。使用该模板的另一个好处是,每次重新打开您创建的文档时,都会在文档的工作区中打开流程图模具。尝试这个:

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