How Can I Insert Text Into an Existing Microsoft Word Bookmark?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I insert text into an existing Microsoft Word bookmark?

— WM

SpacerHey, Scripting Guy! AnswerScript Center

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’re about to see, it’s not. Instead, for some bizarre reason the Scripting Guy who writes this column found himself absolutely incapable of typing the word bookmarks, a word used repeatedly in the script. Instead, try as he might, he always mistyped this as booksmarks. Surprisingly, the script failed each time it encountered yet another instance of booksmarks.

And, yes, that does sound like a bug in Microsoft Word, doesn’t it?

Fortunately, the Scripting Guy who writes this column is not only a lousy typist, but he won’t concede defeat, either. And thus, after several days and several thousand typos, he somehow managed to cobble together the following script:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)

Set objNetwork = CreateObject(“Wscript.Network”) strUser = objNetwork.UserName strDomain = objNetwork.UserDomain strComputer = objNetwork.ComputerName

Set objRange = objDoc.Bookmarks(“UserBookmark”).Range objRange.Text = strUser objDoc.Bookmarks.Add “UserBookmark”,objRange

Set objRange = objDoc.Bookmarks(“DomainBookmark”).Range objRange.Text = strDomain objDoc.Bookmarks.Add “DomainBookmark”,objRange

Set objRange = objDoc.Bookmarks(“ComputerBookmark”).Range objRange.Text = strComputer objDoc.Bookmarks.Add “ComputerBookmark”,objRange

Let’s see if we can figure out how this works, preferably without having to use the word booksmarks – uh, bookmarks. As you can see, we start out simply enough, creating an instance of the Word.Application object and then setting the Visible 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:

Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)

We should probably stop here and talk about Test.doc for a moment. Because WM’s question was about modifying existing … things … Test.doc needs to have a few bookmarks predefined. For our sample script, we’re assuming the document has the following three bookmarks:

•

UserBookmark

•

DomainBookmark

•

ComputerBookmark

In case you’re wondering, our version of Test.doc looks like this, with phrases like <user name> indicating the location of the three bookmarks:

Microsoft Word


As you probably guessed, we’re going to replace the text of the three bookmarks with the logged-on user’s 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 Wscript.Network object and then assign the values of the UserName, UserDomain, and ComputerName properties to the variables strUser, strDomain, and strComputer, respectively. That’s what we do here:

Set objNetwork = CreateObject(“Wscript.Network”)
strUser = objNetwork.UserName
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName

With that done we’re ready to modify some booksmarks. (Yeah, we know: we misspelled it again. But it’s close enough.) To change the text of the bookmark UserBookmark we use this block of code:

Set objRange = objDoc.Bookmarks(“UserBookmark”).Range
objRange.Text = strUser
objDoc.Bookmarks.Add “UserBookmark”,objRange

Like we said, it’s actually pretty easy to locate and modify the text of a bookmark. In the first line of this code block we create a Range object corresponding to the Range property of the bookmark named UserBookmark. To do that, we simply connect to the Bookmarks collection and, more specifically, the bookmark named UserBookmark:

Set objRange = objDoc.Bookmarks(“UserBookmark”).Range

Why do we even need to create a Range object corresponding to the bookmark’s Range? Well, for whatever reason, a bookmark object doesn’t have a Text property; instead, each bookmark has a Range property that, in turn, has a Text property. It’s a roundabout way of doing things, but to change the text of a bookmark we need to change the text of the bookmark’s Range.

And, yes, that sounds 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:

objRange.Text = strUser

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 is a problem (that is, if you’d prefer that the bookmark remain in the document, albeit with the newly-modified text) then leave this line of code in place:

objDoc.Bookmarks.Add “UserBookmark”,objRange

If you don’t care that the bookmark disappears then you can remove the preceding line.

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’s Add 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.

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:

Microsoft Word


Pretty slick, huh?

Incidentally, here’s 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:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”) Set colBookmarks = objDoc.Bookmarks

For Each objBookmark in colBookmarks Wscript.Echo objBookmark.Name Set objRange = objDoc.Bookmarks(objBookMark.Name).Range Wscript.Echo objRange.Text Wscript.Echo objBookmark.Start Wscript.Echo objBookmark.End Wscript.Echo Next

Most of this should be self-explanatory, which is why we won’t bother to explain any of it. If you do have questions you can take a look at the Microsoft Word VBA Language Reference.

We truly hope this answers your question, WM; after all, considering how long it took us to write (or at least to type) the thing, well …. On the other hand, the inability to type the word bookmarks does put the Scripting Guy who writes this column into very rarefied company. For example, did you know that Albert Einstein also had trouble typing the word bookmarks? It’s true: take a look at his proofs for both the general and special theories of relativity: you won’t find a single instance of the word bookmarks. Coincidence? We think not.

0 comments

Discussion is closed.

Feedback usabilla icon