How to determine if document was changed at all?

K

Kees

I need to determine if, after opening, a document is changed just before the
dosument is printed. It is NOT a new document.
I use the "documentbeforeprint" event but then need to detemine if any
changes are made, if so I have to change a date and editor field in the
document as the document isn't the original document anymore.

Can this be done? How?

Regards, Kees.
 
H

Helmut Weber

Hi Kees,

as long as a document wasn't changed,
this returns true:

MsgBox ActiveDocument.Saved

There maybe limits, though,
e.g. if two changes result in exactly the same text,
and of course, if there is some kind of document automation,
fields update etc.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

I almost posted something similiar but then thought the OP might be
looking for something to indicate whether the document was changed
since being opened rather than changed since last being saved. In the
latter case, our suggestion falls short.

I didn't have time to work it out, but it seems he would need to
intercept both the save and save as events, write a document variable
to record the new modified date, compare that to the modified date at
the time the document was opened (put in another variable during an
AutoOpen macro) then if the two variables differrred and .Saved
returned false he could proceed accordingly.

Perhaps you or someone else has already worked something like this out.
 
K

Kees

Helmut,

Soooooo easy, now I think of it. I implemented it and indeed did want I
wanted!
Vielen dank.

Kees.
 
J

Jonathan West

Greg Maxey said:
Helmut,

I almost posted something similiar but then thought the OP might be
looking for something to indicate whether the document was changed
since being opened rather than changed since last being saved. In the
latter case, our suggestion falls short.

I didn't have time to work it out, but it seems he would need to
intercept both the save and save as events, write a document variable
to record the new modified date, compare that to the modified date at
the time the document was opened (put in another variable during an
AutoOpen macro) then if the two variables differrred and .Saved
returned false he could proceed accordingly.

Perhaps you or someone else has already worked something like this out.

It would be a matter of writing an event handler for the
Application_DocumentBeforeSave() event. That would deal with both the Save
and Save As cases.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg Maxey

Helmut,

Perhaps this would do the trick:

Sub AutoOpen()
ActiveDocument.Variables("SavedDate").Value =
ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
ActiveDocument.Saved = True
End Sub
Sub ScratchMacro()
If ActiveDocument.Variables("SavedDate").Value <> _
ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
Then
MsgBox "This document was saved or changed since it was opened"
End If
If Not ActiveDocument.Saved Then
MsgBox "This document was saved or changed since it was opened"
End If
End Sub
 
K

Kees

Greg,

Indeed, I just tested the variable when nothing was changed and it was false
also. Now that's not exactly what I wanted. I sort of assumed that the
variable would be true right after opening as the original "saved" doc still
exists and as soon as I changed something this would turn false. Sort of
"dirty" flag.

Regards, Kees.
 
H

Helmut Weber

Hi Greg,

you are right.

Yes, in a way I was lucky with helping,
by a useless answer, at second thought.

I had that case in the German groups lately,
where someone wanted to place the cursor
at the start of the doc before closing it,
in order to have the cursor there when opening it.
I recommended
activedocument.range(0,0).select
in autoclose,
which made the OP happy,
and left me rather embarrassed. ;-)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Kees,

I didn't have time to extensively test, but try this combination:

Sub AutoOpen()
ActiveDocument.Variables("SavedDate").Value =
ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
ActiveDocument.Saved = True
End Sub

Sub ScratchMacro()
If ActiveDocument.Variables("SavedDate").Value <> _
ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
Then
MsgBox "This document was saved or changed since it was opened."
End If
If Not ActiveDocument.Saved Then
MsgBox "This document was saved or changed since it was opened."
End If
End Sub
 
G

Greg Maxey

Helmut,

LOL. I suppose a disadvantage that infrequent users have using the
@discussions.com address is that we can't reach out to them and correct
our mistakes ;-). I see that Kees is back and I think on the right
track now.
 
G

Greg Maxey

Jonathan,

I have never messed with this sort of thing. I went in help and put
together the following code, but it doesn't seem to work. Can you
advise on what I am missing or doing wrong. Thanks.

This code is in a module in the Document Project:

Sub AutoOpen()
Dim X As New EventsClassModule
Set X.appWord = Word.Application
End Sub


This code is in a Class module name EventsClassModule

Option Explicit
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really want to " _
& "save the document?", vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub

Nothing happens when I open the document and then save it.
 
K

Kees

Greg, Helmut,

Thank you for your assistance Gregs' code seems to do the job twofold.

First, by setting the activedocument.saved to true any change would toggle
this to false.
Second, when the document was saved before printed, the code comparing the
"savedates" will detect this and I could act accordingly.

Thanks again, Kees.
 
K

Kees

Greg,

This should work as I have similar code to trap both beforeprint and
beforesave, the one thing people tend to forget that after you typed this in
you have to execute the macro that defines your classmodule, in your case
AutoOpen(), or close and open the doc and then test it. Just writing the
code is not sufficient.

Regards, Kees.
 
G

Greg Maxey

Kees,

Yep, I sorted that out with the help of Jezebel in a separate post. Glad
your problem is solved.
 

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