Consulting

Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •