I have a Viso 2013 .vstm
file that launches a VBA macro on document creation (template instanciation when a user opens the template manually). This macro populates the created drawing from a datasource. When finished, I would like to save programatically (from VBA) the drawing that has been generated as a .vsdx
file, i.e. with all VBA macros that were used to populate the drawing being removed.
My questions are:
Is it possible to remove all macros programatically from a VBA macro (Visio 2013) which is in the
.vstm
file itself without causing the VBA Macro to fail and if yes, how can I do it ?If 1. is not possible, how can I force programatically Visio to save to
.vsdx
a drawing that has macros (i.e. save ignoring all macros)If 2. is not possible, how can I copy current drawing (everything except macros) to a new Drawing which should then be savable to
.vsdx
?
I have tried the following:
Deleting all lines with
VBProject.VBComponents.Item(index).CodeModule.DeleteLines
causes the macro to fail with "End Function is missing" (I have checked and there is no missingEnd Function
anywhere, my guess is that the macro probably deletes the code that hasn't been executed yet, which in turn causes this error)Save
andSaveEX
do not work either, I get a "VBProjects cannot be saved in macro-free files" error/message, even if I add aApplication.AlertResponse = IDOK
prior to the call toSave
/SaveEx
.
Here follows a sample code.
Private Sub RemoveVBACode()
' If document is a drawing remove all VBA code
' Works fine however execution fails as all code has been deleted (issue 1)
If ActiveDocument.Type = visTypeDrawing Then
Dim i As Integer
With ActiveDocument.VBProject
For i = .VBComponents.Count To 1 Step -1
.VBComponents.Item(i).CodeModule.DeleteLines 1, .VBComponents.Item(i).CodeModule.CountOfLines
Next i
End With
On Error GoTo 0
End If
End Sub
Private Sub SaveAsVSDX(strDataFilePath As String)
RemoveVBACode
Application.AlertResponse = IDOK
' Next line fails at runtime (issue 2), the same occurs when using Save
ThisDocument.SaveAsEx strDataFilePath, visSaveAsWS + visSaveAsListInMRU
Application.AlertResponse = 0
End Sub
The code that starts the execution of the Macro is the following event:
' This procedure runs when a Visio document is
' created. I.e., when the template (.vstm) is opened.
Private Sub Document_DocumentCreated(ByVal Doc As IVDocument)
' ...
SaveAsVSDX (strDataFilePath)
' ...
End Sub