Accessing the Dirty Flag

by Allen Wyatt
(last updated February 7, 2015)

5

Jerry has made a macro for version control that asks the user if she wants to save the document with a new version number. It runs automatically when a document is opened. However, Jerry would prefer to let the macro run only when a document is actually changed or altered in any way by the user. He wonders if there is an event that registers when a document is actually edited? Or is there, perhaps, a property that registers whether a document has been changed?

There is no event, but there is a property. What you want to do is check the Saved property of whatever document you are processing. If the property is True, then there have been no changes. If the property is False, then there are unsaved changes. (In other words, the True of False state of the Saved property indicates whether the document has been saved or not.)

If you want to do your prompting about a version number when the user is closing the document, then you'll want to work with the Document_Close event handler. Modify the event handler so that you check the state of the Saved property, in this way:

Private Sub Document_Close()
    If Not ActiveDocument.Saved Then
        ' Check to see if version should be updated
        ActiveDocument.Save
    End If
End Sub

You'll need, of course, to replace the comment with your specific code for handling what should occur relative to a version number. The Save method is used to save the document under the current name; if you wanted to save it under a new name you would instead use the SaveAs method.

There is one caveat to remember: It is possible for the Saved property to be set to False even though the user didn't actually make a change in the document. For instance, if there are fields in the document and the fields get automatically updated, then the Saved property is set to False. There are a few other situations where the property is changed, as well, but the Saved property is the most accurate way available to determine whether a document as actually changed or not.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13368) applies to Microsoft Word 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of Workbook Links

Excel allows you to easily link information from one workbook to another. If you want to get rid of links that may be within ...

Discover More

Determining the Day of the Year

Need to find out the day of the year for a particular date? It's easy to do if you are using a macro. All you need to do is ...

Discover More

Saving in Multiple Locations

Need to save a workbook in more than one location? Here's a handy macro that can save your workbook in lots of different ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Displaying the Navigation Pane when Opening a Document

The Navigation pane can be a big help in moving around a document. If you want to make sure it is always displayed for your ...

Discover More

Determining the Current Page Number

While your macro is processing the text in your document, you may need a way to determine the current page number where the ...

Discover More

Determining If a Number is Odd or Even

Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 7?

2015-10-09 12:22:43

donna

My college is using something called a "thin client" for the students. I think this is affecting the Word properties, it says document has never been saved, or was created months before the student started it, or was modified but editing time was 0 minutes. I don't know anything about thin clients so don't know what to make of these results


2015-07-06 09:17:04

Rick G.

One of the properties in all Word docs keeps track of the Date & Time that the doc was Last Printed. (In Word 2010 you can see this property by clicking on the "File" tab.) So anytime you print a doc, this property is automatically updated. That sets the "dirty flag" saying the doc was modified (because the property was modified). There is no option to turn off updating that property when you print the doc. (If you don't save the doc after printing it, that property retains the previous time & date printed.)


2015-02-22 20:53:36

Paulo Almeida

My experience is like Jay Scheuerman says. Before MS Word 2007, that functions works well, but since MS Word 2007, it doesn't work well!


2015-02-09 11:20:40

Dr_Tek

How do you remove a dirty flag from a document that does not have a macro (nor do you want one).

We have PRs that we reuse and save with the new PR # when they are from the same vendor. They often pick up a dirty flag although they were initially saved. We want to retain the original
"Date modified", so we do not want to save them when they are reopened. Also, we have found that saving them again does not remove the flag.


2015-02-09 09:34:16

Jay Scheuerman

On a related vein, is there a way to stop Word from seeing the print function as a change? If I merely open, print, and close one of my typical work documents, Word always asks if I want to save changes. Shouldn't setting the printing options for "Update fields before printing" and "Update linked data before printing" to off keep this from happening?


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.