Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Application Event Not working

Status
Not open for further replies.

DMUM

Programmer
Mar 15, 2012
8
US
HI, PLEASE PLEASE anyone, I really need help with this!

I created some code using this example:


Instead of using the Quit event they use, I am using the DocumentBeforeSave Event.

However it is not working. Just in case it will help here is my code:

------------------------------------------------------------------------------
Code:
In Module

option Explixit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
     Set oAppClass.oApp = Word.Application
End Sub

------------------------------------------------------------------------------
-
In a class module called ThisApplication:  

Option Explicit

Public Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAs UI As
Boolean, Cancel As Boolean)

    Call EditingMacroComments

Sub EditingMacroComments()

End Sub
-----------------------------END CODE-----------------------
Ultimately this needs to be a global template

PLEASE HELP, this should not be sooo difficult. Thanks
 


hi,
However it is not working.
What do you mean?

NOTHING happens?

You get an ERROR? If so WHAT error and WHAT message? On WHAT statement"

SOMETHING happens, but not what you expect? WHAT happened and WHAT did you expect?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
How come you have 'Sub EditingMacroComments()' inside the DocumentBeforeSave sub?

Cheers
Paul Edstein
[MS MVP - Word]
 
Sorry I guess I should have been more specific...nothing happens, no errors, no nothing when I click on Save. It should activate the BeforeSave event...well, at least that is what I want it to do.

As far as the code, it i a mistype. I was missing an End Sub.

Code:
In Module

option Explixit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
     Set oAppClass.oApp = Word.Application
End Sub

------------------------------------------------------------------------------
-
In a class module called ThisApplication:  

Option Explicit

Public Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAs UI As
Boolean, Cancel As Boolean)

    Call EditingMacroComments

End Sub

Sub EditingMacroComments()
   More code here.....
End Sub

Thanks
 
Your 'ThisApplication' module seems to be missing the line:
Public WithEvents wdApp As Word.Application
That line should appear right after the 'Option Explicit'.

Have you tested that the DocumentBeforeSave event is firing? For example insert a simple message box like:
Msgbox "!"

Cheers
Paul Edstein
[MS MVP - Word]
 
Ugh! I didn't type it here. Sorry, I do have that. I can't copy paste my code. Typing this from my home pc and code is on my work pc.

In Module

option Explixit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub

------------------------------------------------------------------------------
-
Code:
In a class module called ThisApplication:  

Option Explicit
Public WithEvents wdApp As Word.Application

Public Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAs UI As
Boolean, Cancel As Boolean)

    Call EditingMacroComments

End Sub

Sub EditingMacroComments()
   More code here.....
End Sub

Yes I tested it. I put a stop on the code. Nothing "fires" when I click save

When I run the Module portion by click F5, this portion runs. But that is it.

Thanks

 
I didn't type it here. Sorry, I do have that. I can't copy paste my code. Typing this from my home pc and code is on my work pc.
Maybe you make a copy of the code you're actually using, then post it here. I don't want to waste any more time pointing out 'issues' only to have you reply that what you've posted doesn't reflect your code's reality.

Cheers
Paul Edstein
[MS MVP - Word]
 
I apologize, I understand your frustration and appreciate you hanging in there with me. Here is my code, copied and pasted from my word vbe:

Code:
module
Option Explicit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
    Set oAppClass.oApp = Word.Application
End Sub

Thanks and sorry again

Code:
Class Module

Option Explicit

Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Call EditingMacroComments
End Sub

Private Sub EditingMacroComments()
Dim ChangesFound As Boolean

  More code for actual action - searches document for specific words.

End Sub

 
From what I can see, your code should work. Apart from your call to the 'EditingMacroComments' sub, I have a similar setup in my own system, described as you've described your's, and it works fine. I'm surprised something like:
Code:
Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "!"
End Sub
doesn't do anything. I assume, of course, that you closed Word & re-started ...

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top