问题
I want to create a visio page, add some shapes, store it with a given filename and close it.
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
回答1:
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
来源:https://stackoverflow.com/questions/3910309/save-and-close-visio-documents-visual-basic-macro