Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Event Handler Woes (Word 2000)

    Hello.

    I have an application event handler for Word which contains a DocumentOpen event. The only way I can get it to work is by manually running my Register_Event_Handler:

    Dim X As New EventHandler
    Sub Register_Event_Handler()
    Set X.objWord = Word.Application
    End Sub

    All my code is contained within a global template located in my startup folder. I was under the impression that my Register_Event_Handler would automatically run because it is in a global template. Is it supposed to? My events are located in a class module and my register event handler is in a regular module. What am I doing wrong?

    Here is my event handler:

    Private Sub objWord_DocumentOpen(ByVal objDoc As Document)

    With objDoc.ActiveWindow
    .Caption = .Path + "" + .ActiveWindow.Document.Name
    .View.Type = wdPrintView
    .ActivePane.View.Zoom.Percentage = 100
    .ActivePane.View.ShowAll = True
    .View.TableGridlines = True
    End With

    If objDoc.TrackRevisions = True And objDoc.ShowRevisions = False Then
    objDoc.ShowRevisions = True
    aretval = MsgBox("Track Changes is enabled and the changes were hidden." + Chr(13) + "Do you want to disable TrackChanges and accept or reject all changes?", vbExclamation + vbYesNo, "Track Changes Warning")
    If aretval = vbYes Then
    objDoc.TrackRevisions = False
    Dialogs(wdDialogToolsAcceptRejectChanges).Show
    End If
    End If

    If objDoc.TrackRevisions = False And objDoc.ShowRevisions = False And objDoc.Revisions.Count > 1 Then
    objDoc.ShowRevisions = True
    bretval = MsgBox("There are revisions within the document that are hidden." + Chr(13) + "It is recommend that they be accepted or rejected at this time." + Chr(13) + "Would you like to accept or reject now?", vbExclamation + vbYesNo, "Track Changes Warning")
    End If

    If bretval = vbYes Then
    objDoc.TrackRevisions = False
    Dialogs(wdDialogToolsAcceptRejectChanges).Show
    End If

    End Sub

    Thanks,

    Kyle

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Event Handler Woes (Word 2000)

    You can run your Register_Event_Handler from an AutoExec procedure in your Global Template. All you have to do is create a Sub called AutoOpen in an Ordinary module (not a class module), and put a call to Register_Event_Handler in there.

    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Event Handler Woes (Word 2000)

    The bare fact that Register_Event_Handler is in a global module doesn't mean that it will be run. Imagine that ALL macros in a global template would be executed the moment it is loaded!

    Either rename Register_Event_Handler to AutoExec, or call it from a macro (in a standard module) named AutoExec. Instead of that, you can also call it from the Document_Open event handler in ThisDocument.

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Event Handler Woes (Word 2000)

    hi Kyle,

    "I was under the impression that my Register_Event_Handler would automatically run because it is in a global template. Is it supposed to?"

    no. application level event handlers always need to be instantiated. you can do this when the global template loads by having the instantiation code inside an autoexec sub.

    "My events are located in a class module and my register event handler is in a regular module. What am I doing wrong?"

    nothing, see my previous anwer. an alternative would be to code the thisdocumentlevel document_open event, which will be triggered for each document that is attached to the normal template. this will run by itself and doesn't need explicit instantiation.

  5. #5
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Event Handler Woes (Word 2000)

    Thank you very much.

    Kyle

  6. #6
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Event Handler Woes (Word 2000)

    Thank you Hans. Yes, this make sense now. I renamed my Register_Event_Hander to AutoExec. Thanks so much for your help. One more question for you: Would would be the benefit of calling the Register_Event_Handler from ThisDocument. Would all the documents have to be based off of my global template?

    Kyle

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Event Handler Woes (Word 2000)

    >> Would would be the benefit of calling the Register_Event_Handler from ThisDocum

    Nothing in particular, I just wanted to point out two different ways of doing the same thing. Document_Open in ThisDocument is slightly more "modern"; AutoExec already existed in the time when Word was programmed with WordBasic instead of Visual Basic. But both will work.

    >> Would all the documents have to be based off of my global template?

    No, since a global template is opened automatically when Word is loaded, the AutoExec or Document_Open macro is run once, when Word is opened.

Posting Permissions

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