{"id":63323,"date":"2008-01-09T21:37:00","date_gmt":"2008-01-09T21:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/01\/09\/hey-scripting-guy-how-can-i-reset-the-revision-number-of-a-word-document-to-1\/"},"modified":"2008-01-09T21:37:00","modified_gmt":"2008-01-09T21:37:00","slug":"hey-scripting-guy-how-can-i-reset-the-revision-number-of-a-word-document-to-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-reset-the-revision-number-of-a-word-document-to-1\/","title":{"rendered":"Hey, Scripting Guy! How Can I Reset the Revision Number of a Word Document to 1?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I reset the revision number of a Microsoft Word document back to 1?<\/p>\n<p>&#8212; JL<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, JL. You know, a lot of people think the Scripting Guys have lost their edge. \u201cYou guys used to write all these crazy scripts that used all these weird and wacky workarounds in order to solve problems,\u201d they say. \u201cNow you just write boring old scripts that use straightforward approaches to solving problems. We miss the old Scripting Guys.\u201d <\/p>\n<p>Now, admittedly, we didn\u2019t understand that at first. How could you miss the old Scripting Guys; after all, Scripting Guy Jean Ross is still here, isn\u2019t she? But then we realized that you didn\u2019t mean old as in \u201celderly,\u201d you meant old as in \u201cwild and crazy.\u201d 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\u2019re going to give you a good old-fashioned weird and wacky workaround.<\/p>\n<p>And then some.<\/p>\n<p>Admittedly, we didn\u2019t 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\u2019t get any error messages, but we didn\u2019t 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 \u2013 Author, Subject, Title, etc. \u2013 no problem, but we couldn\u2019t change the revision number. And a brief search of the Web suggested the same thing: you can\u2019t programmatically change the revision number of a Microsoft Word document. That\u2019s something that simply cannot be done.<\/p>\n<p>Well, not unless you use a script like this one:<\/p>\n<pre class=\"codeSample\">Set objWord = CreateObject(\"Word.Application\")\nobjWord.Visible = True\n\nSet objDoc = objWord.Documents.Open(\"C:\\Scripts\\Test.doc\")\n\nSet objSelection = objWord.Selection\nobjSelection.WholeStory\nobjSelection.Copy\n\nobjDoc.Close\n\nSet objNewDoc = objWord.Documents.Add\n\nSet objSelection = objWord.Selection\nobjSelection.Paste\n\nobjNewDoc.SaveAs \"C:\\Scripts\\Test.doc\"\n\nobjWord.Quit\n<\/pre>\n<p>Let\u2019s see if we can explain what\u2019s going on here. As you can see, there\u2019s nothing particularly weird \u2013 or the least bit wacky \u2013 about the first three lines of code; all we do there is create an instance of the <b>Word.Application<\/b> object, set the <b>Visible<\/b> property to True, then use the <b>Open<\/b> method to open the document C:\\Scripts\\Test.doc. So far so good.<\/p>\n<p>After that straightforward beginning, however, things start to get a little odd. As we noted, we couldn\u2019t 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 <i>almost<\/i> worked; unfortunately, though, each time we tried the script it set the revision number to <i>2<\/i>. Close, but no cigar.<\/p>\n<table class=\"dataTable\" id=\"ETD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. 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.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>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\u2019re about to find out.<\/p>\n<p>To try this new approach we began by creating an instance of Word\u2019s <b>Selection<\/b> object. Once we had a Selection object in hand we then called the <b>WholeStory<\/b> method, a method that expands the current selection to encompass the entire story. Which, for all intents and purposes, means selecting the entire document.<\/p>\n<p>After we select the entire document we call the <b>Copy<\/b> method to copy that document to the Clipboard. And then, because we can\u2019t overwrite an open document, we call the <b>Close<\/b> method and close Test.doc.<\/p>\n<p>Got all that? Good. Our next step is to create a new, blank document, one with the object reference objNewDoc; that\u2019s what this line of code is for:<\/p>\n<pre class=\"codeSample\">Set objNewDoc = objWord.Documents.Add\n<\/pre>\n<p>Inside this new document we create a new instance of the Word Selection object, then call the <b>Paste<\/b> method to paste the contents of the Clipboard into the document. The net effect of all that? Everything that was in Test.doc \u2013 and was copied to the Clipboard \u2013 can now be found in our new, unsaved document. Which is exactly where we were hoping it could be found.<\/p>\n<p>The rest is easy. We next call the <b>SaveAs<\/b> method, passing the file path C:\\Scripts\\Test.doc as the method parameter:<\/p>\n<pre class=\"codeSample\">objNewDoc.SaveAs \"C:\\Scripts\\Test.doc\"\n<\/pre>\n<p>What\u2019s that going to do? You got it: that\u2019s 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\u2019s going to have a revision number of 1. That\u2019s something we can verify by calling the <b>Quit<\/b> method to terminate Microsoft Word, then checking the revision number of Test.doc:<\/p>\n<p><img decoding=\"async\" height=\"509\" alt=\"Spacer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/revision.jpg\" width=\"367\" border=\"0\" \/> <\/p>\n<p>It\u2019s weird (and wacky), but it works.<\/p>\n<p>By the way, this approach can only set the revision number back to 1; it can\u2019t set the revision number to, say, 24. <i>Can<\/i> you set the revision number to 24? Well, yeah. But the only way we could figure out to do that is <i>really<\/i> weird: we had to create a new document and then save it 23 times. That might be too wacky even for <i>our<\/i> readers. But it\u2019ll work.<\/p>\n<p>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\u2019re <i>still<\/i> worried that the Scripting Guys will no longer provide you with weird and wacky solutions, well, we have just one thing to say: <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>The 2008 Winter Scripting Games<\/b><\/a>. 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.<\/p>\n<table class=\"dataTable\" id=\"E4F\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Does that mean that the Scripting Guys don\u2019t know anything about Perl? Yes it does; they don\u2019t have the slightest idea how to write scripts using Perl. Does it also mean that, even though they don\u2019t know the first thing about Perl, the Scripting Guys are still going to conduct \u2013 and judge \u2013 a scripting competition that uses Perl? Of course it does. After all, that \u2013 if anything \u2013 is the Scripting Guy way.<\/p>\n<p>Like weird and wacky? Just wait a few weeks; you\u2019ll have all the weird and wacky you could ever ask for.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I reset the revision number of a Microsoft Word document back to 1? &#8212; JL Hey, JL. You know, a lot of people think the Scripting Guys have lost their edge. \u201cYou guys used to write all these crazy scripts that used all these weird and wacky workarounds in order [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-63323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I reset the revision number of a Microsoft Word document back to 1? &#8212; JL Hey, JL. You know, a lot of people think the Scripting Guys have lost their edge. \u201cYou guys used to write all these crazy scripts that used all these weird and wacky workarounds in order [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}