{"id":66403,"date":"2006-09-25T21:36:00","date_gmt":"2006-09-25T21:36:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/25\/how-can-i-insert-text-into-an-existing-microsoft-word-bookmark\/"},"modified":"2006-09-25T21:36:00","modified_gmt":"2006-09-25T21:36:00","slug":"how-can-i-insert-text-into-an-existing-microsoft-word-bookmark","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-text-into-an-existing-microsoft-word-bookmark\/","title":{"rendered":"How Can I Insert Text Into an Existing Microsoft Word Bookmark?"},"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! How can I insert text into an existing Microsoft Word bookmark?<BR><BR>&#8212; WM<\/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, WM. Believe it or not, this turned out to be <I>the<\/I> most difficult script the Scripting Guy who writes this column has ever had to create. Not because the script itself is very hard; as you\u2019re about to see, it\u2019s not. Instead, for some bizarre reason the Scripting Guy who writes this column found himself absolutely incapable of typing the word <I>bookmarks<\/I>, a word used repeatedly in the script. Instead, try as he might, he always mistyped this as <I>booksmarks<\/I>. Surprisingly, the script failed each time it encountered yet another instance of <I>booksmarks<\/I>.<\/P>\n<P>And, yes, that <I>does<\/I> sound like a bug in Microsoft Word, doesn\u2019t it?<\/P>\n<P>Fortunately, the Scripting Guy who writes this column is not only a lousy typist, but he won\u2019t concede defeat, either. And thus, after several days and several thousand typos, he somehow managed to cobble together the following script:<\/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 objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName\nstrDomain = objNetwork.UserDomain\nstrComputer = objNetwork.ComputerName<\/p>\n<p>Set objRange = objDoc.Bookmarks(&#8220;UserBookmark&#8221;).Range\nobjRange.Text = strUser\nobjDoc.Bookmarks.Add &#8220;UserBookmark&#8221;,objRange<\/p>\n<p>Set objRange = objDoc.Bookmarks(&#8220;DomainBookmark&#8221;).Range\nobjRange.Text = strDomain\nobjDoc.Bookmarks.Add &#8220;DomainBookmark&#8221;,objRange<\/p>\n<p>Set objRange = objDoc.Bookmarks(&#8220;ComputerBookmark&#8221;).Range\nobjRange.Text = strComputer\nobjDoc.Bookmarks.Add &#8220;ComputerBookmark&#8221;,objRange\n<\/PRE>\n<P>Let\u2019s see if we can figure out how this works, preferably without having to use the word booksmarks \u2013 uh, bookmarks. As you can see, we start out simply enough, creating an instance of the <B>Word.Application<\/B> object and then setting 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 document C:\\Scripts\\Test.doc:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\n<\/PRE>\n<P>We should probably stop here and talk about Test.doc for a moment. Because WM\u2019s question was about modifying existing \u2026 things \u2026 Test.doc needs to have a few bookmarks predefined. For our sample script, we\u2019re assuming the document has the following three bookmarks:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>UserBookmark<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>DomainBookmark<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>ComputerBookmark<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In case you\u2019re wondering, our version of Test.doc looks like this, with phrases like <I>&lt;user name&gt;<\/I> indicating the location of the three bookmarks:<\/P><IMG border=\"0\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/bookmark1.jpg\" width=\"400\" height=\"338\"> \n<P><BR>As you probably guessed, we\u2019re going to replace the text of the three bookmarks with the logged-on user\u2019s name and domain, as well as the name of the computer he or she is logged on to. To do that, we first create an instance of the <B>Wscript.Network<\/B> object and then assign the values of the <B>UserName<\/B>, <B>UserDomain<\/B>, and <B>ComputerName<\/B> properties to the variables strUser, strDomain, and strComputer, respectively. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName\nstrDomain = objNetwork.UserDomain\nstrComputer = objNetwork.ComputerName\n<\/PRE>\n<P>With that done we\u2019re ready to modify some booksmarks. (Yeah, we know: we misspelled it again. But it\u2019s close enough.) To change the text of the bookmark UserBookmark we use this block of code:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Bookmarks(&#8220;UserBookmark&#8221;).Range\nobjRange.Text = strUser\nobjDoc.Bookmarks.Add &#8220;UserBookmark&#8221;,objRange\n<\/PRE>\n<P>Like we said, it\u2019s actually pretty easy to locate and modify the text of a bookmark. In the first line of this code block we create a <B>Range<\/B> object corresponding to the Range property of the bookmark named UserBookmark. To do that, we simply connect to the <B>Bookmarks<\/B> collection and, more specifically, the bookmark named UserBookmark:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Bookmarks(&#8220;UserBookmark&#8221;).Range\n<\/PRE>\n<P>Why do we even <I>need<\/I> to create a Range object corresponding to the bookmark\u2019s Range? Well, for whatever reason, a bookmark object doesn\u2019t have a Text property; instead, each bookmark has a Range property that, in turn, has a Text property. It\u2019s a roundabout way of doing things, but to change the text of a bookmark we need to change the text of the bookmark\u2019s Range.<\/P>\n<P>And, yes, that <I>sounds<\/I> convoluted, but, fortunately, this is a case of easier done than said. In fact, this one line of code is all we need to set the text value of the bookmark UserBookmark to the value of the variable strUser:<\/P><PRE class=\"codeSample\">objRange.Text = strUser\n<\/PRE>\n<P>The third line in our code block is optional; depending on your needs you can leave it out altogether. By default, any time you change the text of a bookmark you delete the bookmark itself. That might or might not be a problem. If it <I>is<\/I> a problem (that is, if you\u2019d prefer that the bookmark remain in the document, albeit with the newly-modified text) then leave this line of code in place:<\/P><PRE class=\"codeSample\">objDoc.Bookmarks.Add &#8220;UserBookmark&#8221;,objRange\n<\/PRE>\n<P>If you don\u2019t care that the bookmark disappears then you can remove the preceding line.<\/P>\n<P>Oh, right: we should probably mention that the line of code in question simply recreates the bookmark UserBookmark, something it does by calling the collection\u2019s <B>Add<\/B> method and passing two parameters: the name of the new bookmark and the object variable objRange, which indicates the range (location in the document) for the bookmark.<\/P>\n<P>The remainder of the script simply follows the exact same process in order to modify the text of DomainBookmark and ComputerBookmark. When all is said and done Test.doc should look something like this:<\/P><IMG border=\"0\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/bookmark2.jpg\" width=\"400\" height=\"338\"> \n<P><BR>Pretty slick, huh?<\/P>\n<P>Incidentally, here\u2019s another little script you might find handy. This one opens the document Test.doc and then reports back information about all the bookmarks found in that 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;)\nSet colBookmarks = objDoc.Bookmarks<\/p>\n<p>For Each objBookmark in colBookmarks\n    Wscript.Echo objBookmark.Name\n    Set objRange = objDoc.Bookmarks(objBookMark.Name).Range\n    Wscript.Echo objRange.Text\n    Wscript.Echo objBookmark.Start\n    Wscript.Echo objBookmark.End\n    Wscript.Echo\nNext\n<\/PRE>\n<P>Most of this should be self-explanatory, which is why we won\u2019t bother to explain any of it. If you <I>do<\/I> have questions you can take a look at the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbawd11\/html\/woobjBookmark1_HV05210072.asp\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A>.<\/P>\n<P>We truly hope this answers your question, WM; after all, considering how long it took us to write (or at least to <I>type<\/I>) the thing, well \u2026. On the other hand, the inability to type the word bookmarks <I>does<\/I> put the Scripting Guy who writes this column into very rarefied company. For example, did you know that Albert Einstein <I>also<\/I> had trouble typing the word bookmarks? It\u2019s true: take a look at his proofs for both the general and special theories of relativity: you won\u2019t find a single instance of the word bookmarks. Coincidence? We think not.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I insert text into an existing Microsoft Word bookmark?&#8212; WM Hey, WM. Believe it or not, this turned out to be the most difficult script the Scripting Guy who writes this column has ever had to create. Not because the script itself is very hard; as you\u2019re about to see, [&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,395],"class_list":["post-66403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I insert text into an existing Microsoft Word bookmark?&#8212; WM Hey, WM. Believe it or not, this turned out to be the most difficult script the Scripting Guy who writes this column has ever had to create. Not because the script itself is very hard; as you\u2019re about to see, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66403","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=66403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66403\/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=66403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}