{"id":63913,"date":"2007-09-29T02:20:00","date_gmt":"2007-09-29T02:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/09\/29\/how-can-i-retrieve-field-values-in-a-microsoft-word-document\/"},"modified":"2007-09-29T02:20:00","modified_gmt":"2007-09-29T02:20:00","slug":"how-can-i-retrieve-field-values-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-field-values-in-a-microsoft-word-document\/","title":{"rendered":"How Can I Retrieve Field Values in a Microsoft Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I have a bunch of Word documents that include fields like LastSavedBy. How can I write a script that retrieves the current values for these fields?<BR><BR>&#8212; OS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, OS. You know, the other night the Scripting Guy who writes this column was watching TV when he saw an ad for a new Monopoly game: <A href=\"http:\/\/www.hasbro.com\/games\/kid-games\/monopoly\/default.cfm?page=Products\/Detail&amp;product_id=19783\" target=\"_blank\"><B>Monopoly, the Electronic Banking Edition<\/B><\/A>. What was interesting (or disturbing, depending on your point of view) is that this version of Monopoly does away with cash; instead, players use debit cards and an ATM-like machine to keep track of their winnings. <\/P>\n<P>In case you\u2019re wondering, the Scripting Guy who writes this column found this disturbing. After all, there are few things in life more satisfying than watching one of your brothers grimly counting out every last Monopoly dollar he has, desperately hoping to reach the $1000 in rent he owes you. (And all the while you saying, \u201cTell you what, you give me your two yellow properties and you won\u2019t owe me a thing.\u201d) Having him simply swipe his debit card doesn\u2019t sound like much fun at all. Sitting there with a huge pile of cash in front of you is absolutely <I>the<\/I> greatest feeling in the world.<\/P>\n<P>Just ask \u2013 uh, never mind. There are certain people even the Scripting Guys shouldn\u2019t make jokes about.<\/P>\n<TABLE id=\"EID\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Sign of the times<\/B>. Technically, the game doesn\u2019t use just any old debit card: it\u2019s a <I>Visa<\/I> debit card. Also, the playing pieces have been updated for the Banking Edition; these pieces now include: a Segway personal transporter (which is nice; after all, most of us will never see a Segway in real life); a space shuttle; a flat-screen TV; a baseball cap; a dog in handbag (yes, a handbag); and \u2013 the piece everyone will likely fight over \u2013 a tin of Altoids.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, the Scripting Guy who writes this column has no intention of playing the Electronic Banking Edition; he much prefers the old-fashioned version. And he will never, <I>ever<\/I> be the tin of Altoids. Instead, he prefers to be the race car any time he plays Monopoly. But, then again, who doesn\u2019t?<\/P>\n<TABLE id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Sigh. As you might expect, the <I>Scripting <\/I><I>Ed<\/I><I>itor<\/I> doesn\u2019t like being the race car; she prefers to be the shoe. (You heard right: the shoe.) We assume that\u2019s only because the game doesn\u2019t have any playing pieces even <I>more<\/I> boring than the shoe.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But it doesn\u2019t really matter; after all, who has time for games anyway? Not us, not when we need to write a script that can retrieve field values from a Microsoft Word document: <\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)<\/p>\n<p>Set colFields = objDoc.Fields\ncolFields.Update<\/p>\n<p>For Each objField in colFields\n    Select Case objField.Type\n        Case 20\n        Set objRange = objField.Result\n        strUser = objRange.Text\n        Wscript.Echo &#8220;Last Saved By: &#8221; &amp; strUser\n    End Select\nNext\n<\/PRE>\n<P>Let\u2019s meander through the code and see if we can figure out how this all works. To begin with, we create an instance of the <B>Word.Application<\/B> object and then set the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Word that we can see onscreen. We then use this line of code to open the file C:\\Scripts\\Test.doc:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\n<\/PRE>\n<P>Well, that was easy enough, wasn\u2019t it? Like taking candy from a baby.<\/P>\n<P>Or playing Monopoly with your brothers.<\/P>\n<P>After we open the document we then use the following line of code to retrieve a collection of all the fields in that document:<\/P><PRE class=\"codeSample\">Set colFields = objDoc.Fields\n<\/PRE>\n<P>Once we have the collection in hand we call the <B>Update<\/B> method to update each of these fields. By calling the Update method we get the latest, most up-to-the-minute value for each field in the document; we\u2019re assuming that this is what most of you will want to do. On the other hand, some of you might want to retrieve the value of the fields at the time the document was last saved. That\u2019s fine; just comment out (or remove) the following line of code and your script will <I>not<\/I> update all the fields when it opens the document. The commented code would end up looking like this:<\/P><PRE class=\"codeSample\">&#8216; colFields.Update\n<\/PRE>\n<P>Our next task involves setting up a For Each loop to loop through the collection of fields. As it turns out, Microsoft Word identifies fields by their <B>Type<\/B>, the Type property being a unique integer value. For example, the LastSavedBy field has a value of 20; the Author field has a value of 17. In our script, we set up a Select Case statement to examine the value of the Type property and then echo back the appropriate field name. As you can see, our Select Case statement only looks for one particular field type: the LastSavedBy field, which has a value of 20. However, you can easily extend the Select Case statement to account for other field types. For example, add this line of code to check for \u2013 and echo back the value of \u2013 the Author field:<\/P><PRE class=\"codeSample\">Select Case objField.Type\n    Case 17\n    Set objRange = objField.Result\n    strUser = objRange.Text\n    Wscript.Echo &#8220;Author: &#8221; &amp; strUser\n<\/PRE>\n<P>Or, just use this code to echo back all the values, without specifying the field type:<\/P><PRE class=\"codeSample\">For Each objField in colFields\n    Set objRange = objField.Result\n    Wscript.Echo objRange.Text\nNext\n<\/PRE>\n<P>We\u2019ll leave that up to you. If you don\u2019t need to echo back the field Type, or if you, say, want to be the <I>shoe<\/I>, well, hey, to each his own.<\/P>\n<TABLE id=\"EVF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Good point: how <I>are<\/I> you supposed to know that the Author field has a Type equal to 17? Well, probably the best way is to go to the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/Bb213727.aspx\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A> on MSDN and take a peek at the <B>wdFieldType<\/B> enumeration.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s assume that we do find the Last Saved By field (a field that has a Type equal to 20). In that case, we execute this block of code:<\/P><PRE class=\"codeSample\">Set objRange = objField.Result\nstrUser = objRange.Text\nWscript.Echo &#8220;Last Saved By: &#8221; &amp; strUser\n<\/PRE>\n<P>This is a little goofy, but in order to echo the value of the field we first need to create a <B>Range<\/B> object encompassing the field\u2019s <B>Result<\/B> (that is, its value). We use this line of code to create the Range object:<\/P><PRE class=\"codeSample\">Set objRange = objField.Result\n<\/PRE>\n<P>Once we have a Range object we can then assign the value of that object\u2019s <B>Text<\/B> property to a variable named strUser. And then we can simply echo back the message \u201cLast Saved By: \u201c along with the name of the person who last saved the document:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8221; Last Saved By: &#8221; &amp; strUser\n<\/PRE>\n<P>And that, as they say, is that.<\/P>\n<P>As you might expect, when he saw the commercial for Monopoly: The Electronic Banking Edition the Scripting Guy who writes this column immediately thought, \u201cWhat are they trying to do, ruin the game completely?\u201d Interestingly enough, after doing a little research he discovered that this isn\u2019t the first time Parker Brothers has tried to ruin the game completely. In 1934 Charles Darrow attempted to sell his new creation to the company; Parker Brothers turned him down, claiming that the game took too long to play and was way too complicated. Darrow thus set out on his own, and the game began selling like hotcakes. Needless to say, Parker Brothers reconsidered and, a year later, bought the rights.<\/P>\n<P>Monopoly quickly became the best-selling game Parker Brothers had ever had; in fact, its sales were credited with saving the company from bankruptcy. So what did Parker Brothers do? In 1936 they decided to stop making the game, assuming that it was just a fad that would soon die out. A sharp spike in sales caused them to change their mind, and from that day on\u2013 well, you know the rest of the story.<\/P>\n<TABLE id=\"EAH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Believe it or not, the Scripting Guy who writes this column didn\u2019t know the history of Monopoly right off the top of his head; as we noted, he had to go out and research this. And yes, that <I>is<\/I> one of the perks about being a Scripting Guy: you can waste time reading about the history of Monopoly and get paid for doing so! After all, it\u2019s research, right?<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a bunch of Word documents that include fields like LastSavedBy. How can I write a script that retrieves the current values for these fields?&#8212; OS Hey, OS. You know, the other night the Scripting Guy who writes this column was watching TV when he saw an ad for a new [&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-63913","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! I have a bunch of Word documents that include fields like LastSavedBy. How can I write a script that retrieves the current values for these fields?&#8212; OS Hey, OS. You know, the other night the Scripting Guy who writes this column was watching TV when he saw an ad for a new [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63913","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=63913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63913\/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=63913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}