Consulting

Results 1 to 4 of 4

Thread: VBA Coding: Event register handler for this document only

  1. #1
    VBAX Newbie
    Joined
    Oct 2020
    Location
    The Netherlands
    Posts
    2
    Location

    VBA Coding: Event register handler for this document only

    Hi,

    I have a document which needs to do a couple of actions before it is saved.
    I have a Macro for that. It works fine, the only issue is, that it is also triggered in all other open documents and that is not what I want.
    It only needs to be triggered in the document itself.

    The code I am using:

    Microsoft Word Objects:
    ThisDocument:
    Private Sub Document_New()
    Register_Event_Handler
    End Sub
    Private Sub Document_Open()
    Register_Event_Handler
    End Sub


    Module:
    mdlEventConnect
    Dim X As New EventClassModule
    Sub Register_Event_Handler()
    Set X.App = Word.Application
    End Sub

    Class Modules:
    EventClassModule
    Public WithEvents App As Word.Application
    ___________________________________________________________________________ ________
    Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)


    Dim intResponse As Integer

    'Do you want to save question?
    intResponse = MsgBox("Do you really want to save the document? ", vbYesNo)


    If intResponse = vbNo Then Cancel = True

    End Sub

    Attached a word document with my code.

    Does anybody know how that can be achieved?

    Thanks in advance
    Olaf
    Attached Files Attached Files

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,399
    Location
    Try this

    I tweaked the macros a little so I could trace things

    Option Explicit
    
    
    Public WithEvents App As Word.Application
    
    
    Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
        Dim intResponse As Integer
     
        If ActiveDocument Is ThisDocument Then
            'Do you want to save question?
            If MsgBox("Do you R-E-E-E-ALLY want to save the document?", vbYesNo) = vbNo Then Cancel = True
        End If
     
     End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Oct 2020
    Location
    The Netherlands
    Posts
    2
    Location
    It works now.
    So simple, but it didn't cross my mind.

    Many Thanks Paul.

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,399
    Location
    Good thing I've got a simple mind
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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