Run macro when visio closes

I need to run a macro when closing visio, and I can't seem to find the appropriate documentation to get me there.  Any help would be appreciated.
Who is Participating?
oldvikingConnect With a Mentor Commented:
I tried this myself, and it worked fine with Visio 2007.

But please check that you have enabled macros under Tools, Trust center, Macro settings, and restarted Visio afterwards.
Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue ?"    ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
Title = "MsgBox Demonstration"    ' Define title.
Help = "DEMO.HLP"    ' Define Help file.
Ctxt = 1000    ' Define topic
        ' context.
        ' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then    ' User chose Yes.
    MyString = "Yes"    ' Perform some action.
Else    ' User chose No.
    MyString = "No"    ' Perform some action.
End If
End Sub

Open in new window

I hope this will bring you a bit further:
If you are working with a specific file, you can add a "Before Document Close" event.
Enter the VBA editor, find the "This document" section in the project explorer, and select the BeforeDocumentClose event in the dropdown to the far right.
WINAIRMVSAuthor Commented:
I added the code to "This Document" which did not act on the closing of the document.
Private Sub BeforeDocumentClose()

Open in new window

WINAIRMVSAuthor Commented:
Answer was what I needed.  There was some extra code that wasn't necessary for what I was looking for, but otherwise great answer!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.