DocumentBeforeSave

R

Rich

I'm trying to use the above function in a macro for Words, but can't get it
to work. I've used this before in Excel, but Word is different.
 
J

Jean-Guy Marcil

Rich was telling us:
Rich nous racontait que :
I'm trying to use the above function in a macro for Words, but can't
get it to work. I've used this before in Excel, but Word is
different.

You are not telling us much here...

What problems are you facing?
What have you tried?
What code do you have so far?
What are you trying to achieve (expectations)?
What are the results?
What Word version are you working with?


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rich

Hi Jean,

What problems are you facing?
What have you tried?
What code do you have so far?
What are the results?
===============================
The macro does not initiate when I go to save. I’ve experimented with just
a simple task for error trapping purposes…

Public Sub app_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As
Boolean, Cancel As Boolean)
MsgBox ("Action1 was triggered")
End Sub

The specified message does not appears prior to the document saving.

What are you trying to achieve (expectations)?
===============================
Prior to the document saving, I would like the program to check some
bookmarks I’ve identified. I have not gotten this far into the programming,
because I can’t get the first step to work.

What Word version are you working with?
==================
2000

Thanks for your inputs.
 
J

Jean-Guy Marcil

Rich was telling us:
Rich nous racontait que :
Hi Jean,

What problems are you facing?
What have you tried?
What code do you have so far?
What are the results?
===============================
The macro does not initiate when I go to save. I've experimented
with just a simple task for error trapping purposes.

Public Sub app_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As
Boolean, Cancel As Boolean)
MsgBox ("Action1 was triggered")
End Sub

The specified message does not appears prior to the document saving.

Have you initialized the application event with a Class module first?
See:
http://word.mvps.org/faqs/macrosvba/AppClassEvents.htm

If so, can you post all modules you are using so I can test them as they
are?

What are you trying to achieve (expectations)?
===============================
Prior to the document saving, I would like the program to check some
bookmarks I've identified. I have not gotten this far into the
programming, because I can't get the first step to work.

OK, when you get there, we can see what can be done!
What Word version are you working with?
==================
2000
--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rich

Here are the other two modules:

Public Sub Class_Initialize()
Set app = Application
End Sub
================
Sub Register_Event_Handler()
Set Action1 = Word.Application
End Sub
 
J

Jean-Guy Marcil

Rich was telling us:
Rich nous racontait que :
Here are the other two modules:

Public Sub Class_Initialize()
Set app = Application
End Sub
================
Sub Register_Event_Handler()
Set Action1 = Word.Application
End Sub


What is Action1? I do not see it declared anywhere.
What kind of modules are these?
What is the name of the Class module?

Did you actually read the web page I recommended?
I think you should... I could give you the code you need, but because
application events can be confusing, I think it is important that you
understand what is going on. The page I recommended is very well written and
if you follow the instructions one strep at the time, you will get what you
want in no time at all. Especially if you are coming from Excel where all
the work has been done under the hood.... In Word you have to create the
whole code for the event from scratch.

Just two comments regarding the web page.

1)
In step 4 and 5, it is assumed that you are producing a global template.
While that makes sense for application wide events, that may not be the case
( I have had to use Application wide events that applied only to a sub-set
of documents based on a few templates).

Anyway, if the template is not going to be used as a global one, then the
first thing you have to do is change the AutoExec macro to one macro (Or two
macros if you are using a template):

'_______________________________________
Private Sub Document_Open()

Set oAppClass.oApp = Word.Application

End Sub
'_______________________________________

and possibly:

'_______________________________________
Private Sub Document_New()

Set oAppClass.oApp = Word.Application


End Sub
'_______________________________________

This way, the class module will be initialize when a document is opened or
created form the template, even if it is not in the start-up folder.

If you are not creating a global template, you have to be aware of the fact
that whatever code you write will be activated for all documents after
initializing the class module. You may want to add code to make sure that
the code only executes for the documents you want.

2)
In step 6, select the BeforeSave event and write your code there.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rich

Hi Jean,

I don’t want this template to be global, so I imbedded the code into my Word
document. The problem now is, I have to manually run the AutoExec macro to
get this to work.

I saw your suggestion in the previous email that I must change the AutoExec
macro, but where should I place your suggested macros, Module 1,
ThisApplication, etc.?

Again, my goal is to have DocumentBeforeSave triggered and be transparent to
the user, as in Excel’s version.


Project (Document1)

Module 1 inside Modules
Option Explicit
Dim oAppClass As New ThisApplication

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

End Sub

ThisApplication inside Class Modules

Option Explicit
Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As
Boolean, Cancel As Boolean)
MsgBox ("This was triggered prior to saving")
End Sub
 
J

Jean-Guy Marcil

Rich was telling us:
Rich nous racontait que :
Hi Jean,

I don't want this template to be global, so I imbedded the code into
my Word document. The problem now is, I have to manually run the
AutoExec macro to get this to work.

I saw your suggestion in the previous email that I must change the
AutoExec macro, but where should I place your suggested macros,
Module 1, ThisApplication, etc.?

In the ThisDocument class module.
Again, my goal is to have DocumentBeforeSave triggered and be
transparent to the user, as in Excel's version.

As I wrote in my previous post, because you are now creating an Application
event , the code you write will fire for every document that may be saved
after the "DocumentBeforeSave" document was opened, if it is still opened.
You may need to add code to prevent the code from executing in other
documents.

But, if all this code is for only one document, then it is a little
overkill.

You could intercept the Save command instead:
Intercept the Save and Save As commands with macros named like this:

'_______________________________________
Sub FileSave()

End Sub
'_______________________________________

'_______________________________________
Sub FileSaveAs()

End Sub
'_______________________________________

Use code to do the save, and then do what you want to do.
Normally, I use a third sub that actually does the work:

'_______________________________________
Sub FileSave()

SaveEvent 1

End Sub
'_______________________________________

'_______________________________________
Sub FileSaveAs()

SaveEvent 2

End Sub
'_______________________________________
'_______________________________________
Sub SaveEvent(lngSaveType As Long)

If lngSaveType = 1 Then
ActiveDocument.Save
Else
Dialogs(wdDialogFileSaveAs).Show
'In fact, you need more code here in case the user cancels out of that
dialog,
' you have to code accordingly
'See the VBA help file on Dialogs. Or do a Search in Google Groups,
'there are may example on how to do this
'If the document was not successfully saved with Save As:
Exit Sub
End if

'Do the stuff you want to do after the save here

End Sub
'_______________________________________


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top