detect if Word has been edited

T

Terry

Hi,

I am opening word via VB (code below) and need to know if the user has made
any edits to the doc.
I know I can use wordbasic.isdocumentdirty() in a VBA as below,

Private Sub Document_Close()
If WordBasic.IsDocumentDirty() Then
MsgBox "Doc is dirty"
Else
MsgBox "Doc is not dirty"
End If
End Sub

But I do not want to use a VBA module within the Word doc if I can avoid it.

Is there a way for the calling VB exe to determine IsDocumentDirty() ?
VB code follows:

Dim objApp As New Word.Application
Dim objDoc As Word.Document

objApp.Documents.Open FileName:="C:\temp\test.doc",
AddToRecentFiles:=False

objApp.Visible = True
objApp.Activate
Set objDoc = objApp.ActiveDocument


MsgBox "Click to continue"
objDoc.Close
 
J

Jay Freedman

The WordBasic.IsDocumentDirty() function is obsolete. The VBA
equivalent is the document's .Saved property -- although the logic is
reversed, so .Saved is true when IsDocumentDirty() would return False,
and vice versa.

If Not objDoc.Saved Then
MsgBox "This document has been edited"
End If

By the way, I don't think the way you're opening and assigning the
document is good. First activate the application object and make it
visible, then use

Set objDoc = objApp.Documents.Open(FileName:="C:\temp\test.doc",
AddToRecentFiles:=False)

and avoid the use of objApp.ActiveDocument. The problem with
ActiveDocument is that while you might get objDoc pointing to test.doc
in most situations, there's no guarantee that it won't point to some
other open document.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
H

Helmut Weber

Hi Terry,

you may determine if there were changes
made in a document by checking whether
it was saved, but see below!

If activedocument.saved = false

Though not all changes make a document dirty, e.g. changes like

ActiveDocument.BuiltInDocumentProperties("Author").Value = "xxx"

But the entire idea is useless
when trying to check if a just opened document is dirty.
That would always be false, unless there were automatic
changes made just after or during opening it.

activedocument.saved checks whether the actual state
of the doc is different from last save.

IMHO.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Andrew Kennard

Check the Saved property for the active document

If it's True the document is NOT dirty

HTH

Andrew
 
K

kozaw

Check the Saved property for the active document

If it's True the document is NOT dirty

HTH

Andrew


Yes, it is as simple as..

Private Sub CheckingDocSavedOrNot()
If Activedocument.saved = False Then
MsgBox "Doc is dirty"
' you can do saving by >> Activedocument.save
Else
MsgBox "Doc is not dirty"
End If
End Sub

KoZaw
 

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