Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I create or handle events in a drawing?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I create or handle events in a drawing?

    There are times when you may need to capture certain events from Visio so you can intercept, add some processing, logging, security, etc. The WithEvents keyword creates a event handler for the object type that is declared with it. You will have to set a new instance of the event object variable before it can be used.

    If you create the event procedures in Visio VBA then you dont need the WithEvents keyword since you are in its environment (the same thread process). Simply pull down the events and let the VBA IDE create them for you.


    Here is a basic example of setting up some events to trigger depending on certain actions in Visio.

    Visio 2003 VBA

    VB Code:
    1. Option Explicit
    2. 'Behind ThisDocument
    3. Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)
    4.     MsgBox "Document About to Close"
    5. End Sub
    6.  
    7. Private Sub Document_BeforeDocumentSave(ByVal doc As IVDocument)
    8.     MsgBox "Document About to be Saved"
    9. End Sub
    10.  
    11. Private Sub Document_DocumentCloseCanceled(ByVal doc As IVDocument)
    12.     MsgBox "Document Closing Canceled"
    13. End Sub
    14.  
    15. Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    16.     MsgBox "Document Opened"
    17. End Sub
    18.  
    19. Private Function Document_QueryCancelDocumentClose(ByVal doc As IVDocument) As Boolean
    20.     MsgBox "Document Closing About to be Canceled"
    21. End Function


    Visio 2003 And VB 6 Code Example
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Visio xx.0 Object Library
    3.  
    4. 'Create an event handler for the document object
    5. Public WithEvents moVsd As Visio.Document
    6.  
    7. 'Create a form level object variable for the application object
    8. Private moApp As Visio.Application
    9.  
    10. Private Sub Command1_Click()
    11.     'Attempt to close the drawing
    12.     moVsd.Close
    13.     'Cancel the close
    14.     moVsd_QueryCancelDocumentClose = True
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18.     Set moApp = New Visio.Application
    19.     'Open the Visio File
    20.     Set moVsd = moApp.Documents.Open("C:\Development\Tips\Visio FAQ - Events\Drawing1.vsd")
    21.     moApp.Visible = True
    22. End Sub
    23.  
    24. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    25.     'Clean up ovject variables
    26.     If moVsd Is Nothing Then
    27.         moVsd.Close
    28.     End If
    29.     Set moVsd = Nothing
    30.     If moApp Is Nothing Then
    31.         moApp.Quit
    32.     End If
    33.     Set moApp = Nothing
    34. End Sub
    35.  
    36. 'Visio Event Procedures
    37. Private Sub moVsd_BeforeDocumentClose(ByVal doc As Visio.IVDocument)
    38.     Me.SetFocus
    39.     MsgBox "Document About to Close"
    40. End Sub
    41.  
    42. Private Sub moVsd_DocumentCloseCanceled(ByVal doc As Visio.IVDocument)
    43.     Me.SetFocus
    44.     MsgBox "Document Close Canceled"
    45. End Sub
    46.  
    47. Private Sub moVsd_DocumentSaved(ByVal doc As Visio.IVDocument)
    48.     Me.SetFocus
    49.     MsgBox "Document Saved"
    50. End Sub
    51.  
    52. Private Function moVsd_QueryCancelDocumentClose(ByVal doc As Visio.IVDocument) As Boolean
    53.     Me.SetFocus
    54.     MsgBox "Query Document Close Canceled"
    55. End Function
    56.  
    57. Private Sub moVsd_ShapeAdded(ByVal Shape As Visio.IVShape)
    58.     Me.SetFocus
    59.     MsgBox "Shape Added"
    60. End Sub
    Last edited by RobDog888; Aug 8th, 2006 at 12:42 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width