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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Using Text Boundaries

Text boundaries can help you better visualize where text can appear in your document. The feature is easy to turn on and ...

Discover More

Dynamic Data Based on Chart Changes

Change the data on which a chart is based and Excel obligingly updates the chart to reflect the change. What if you want ...

Discover More

Saving Grammar Preferences with a Document

The grammar checking tool in Word can be helpful in developing a finished, polished document. You may want to share your ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Displaying a Message in the Status Bar

A great place for your macro to display status information is, well, in the status bar. Displaying the information is ...

Discover More

Controlling the Bold Text Attribute

When processing a document in a macro, you may need to make some of your text bold. It's easy to do using the Bold ...

Discover More

Finding and Changing Word's Internal Commands

If you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...

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 6Mpixels. 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 5 + 3?

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.