Hey, Scripting Guy! How can I reset the revision number of a Microsoft Word document back to 1?
Hey, JL. You know, a lot of people think the Scripting Guys have lost their edge. “You guys used to write all these crazy scripts that used all these weird and wacky workarounds in order to solve problems,” they say. “Now you just write boring old scripts that use straightforward approaches to solving problems. We miss the old Scripting Guys.”
Now, admittedly, we didn’t understand that at first. How could you miss the old Scripting Guys; after all, Scripting Guy Jean Ross is still here, isn’t she? But then we realized that you didn’t mean old as in “elderly,” you meant old as in “wild and crazy.” Well, in that case, we have good news for those of you who think the Scripting Guys have lost their edge. You want weird and wacky workarounds? Today we’re going to give you a good old-fashioned weird and wacky workaround.
And then some.
Admittedly, we didn’t start out with the intention of finding a weird and wacky workaround to this question; on the contrary, considering how remarkably tired we are, we were hoping to answer this question as quickly as possible and then go home and take a nap. Unfortunately, though, the minute we tried changing the revision number programmatically we ran into problems; we didn’t get any error messages, but we didn’t change the revision number, either. Although we tried several different approaches, each of our scripts ended up doing the same thing: nothing. We could change other document properties – Author, Subject, Title, etc. – no problem, but we couldn’t change the revision number. And a brief search of the Web suggested the same thing: you can’t programmatically change the revision number of a Microsoft Word document. That’s something that simply cannot be done.
Well, not unless you use a script like this one:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc") Set objSelection = objWord.Selection objSelection.WholeStory objSelection.Copy objDoc.Close Set objNewDoc = objWord.Documents.Add Set objSelection = objWord.Selection objSelection.Paste objNewDoc.SaveAs "C:\Scripts\Test.doc" objWord.Quit
Let’s see if we can explain what’s going on here. As you can see, there’s nothing particularly weird – or the least bit wacky – about the first three lines of code; all we do there is create an instance of the Word.Application object, set the Visible property to True, then use the Open method to open the document C:\Scripts\Test.doc. So far so good.
After that straightforward beginning, however, things start to get a little odd. As we noted, we couldn’t figure out a clean and simple way to programmatically change the revision number of a document. Being too stubborn to simply give up and tackle an easier question, we tried using the SaveAs method to save the document under a new name; our idea was that we would save the document as, say, Test1.doc, then have the script rename the file back to Test.doc. Believe it or not, that almost worked; unfortunately, though, each time we tried the script it set the revision number to 2. Close, but no cigar.
Note. Anyone out there have a need to set the revision number of a document back to 2? If so, drop us a line; we have a solution ready for you.
And then something happened that had never happened to any of the Scripting Guys before: we got an idea. Suppose we opened the existing document, copied the contents to the Clipboard, pasted those contents into a brand-new document, then saved that new, never-before-saved document as Test.doc? Would that do the trick? We’re about to find out.
To try this new approach we began by creating an instance of Word’s Selection object. Once we had a Selection object in hand we then called the WholeStory method, a method that expands the current selection to encompass the entire story. Which, for all intents and purposes, means selecting the entire document.
After we select the entire document we call the Copy method to copy that document to the Clipboard. And then, because we can’t overwrite an open document, we call the Close method and close Test.doc.
Got all that? Good. Our next step is to create a new, blank document, one with the object reference objNewDoc; that’s what this line of code is for:
Set objNewDoc = objWord.Documents.Add
Inside this new document we create a new instance of the Word Selection object, then call the Paste method to paste the contents of the Clipboard into the document. The net effect of all that? Everything that was in Test.doc – and was copied to the Clipboard – can now be found in our new, unsaved document. Which is exactly where we were hoping it could be found.
The rest is easy. We next call the SaveAs method, passing the file path C:\Scripts\Test.doc as the method parameter:
What’s that going to do? You got it: that’s going to overwrite the existing version of Test.doc with the new version. And because this is the first time this version has ever been saved, it’s going to have a revision number of 1. That’s something we can verify by calling the Quit method to terminate Microsoft Word, then checking the revision number of Test.doc:
It’s weird (and wacky), but it works.
By the way, this approach can only set the revision number back to 1; it can’t set the revision number to, say, 24. Can you set the revision number to 24? Well, yeah. But the only way we could figure out to do that is really weird: we had to create a new document and then save it 23 times. That might be too wacky even for our readers. But it’ll work.
We hope that helps, JL, and we hope that reassures all of you who were afraid the Scripting Guys had lost their edge. If you’re still worried that the Scripting Guys will no longer provide you with weird and wacky solutions, well, we have just one thing to say: The 2008 Winter Scripting Games. If you want weird and wacky solutions, the Scripting Games are bound to include a whole bunch of them. Especially in the two Perl divisions.
Note. Does that mean that the Scripting Guys don’t know anything about Perl? Yes it does; they don’t have the slightest idea how to write scripts using Perl. Does it also mean that, even though they don’t know the first thing about Perl, the Scripting Guys are still going to conduct – and judge – a scripting competition that uses Perl? Of course it does. After all, that – if anything – is the Scripting Guy way.
Like weird and wacky? Just wait a few weeks; you’ll have all the weird and wacky you could ever ask for.