How Can I Open Word with the Cursor Positioned at the Start of a Specified Line?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I open Word with the cursor positioned at the start of a specified line?

— PS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PS. You know, we Scripting Guys have decided that we don’t like Microsoft Word anymore. How come? Well, as Scripting Guys we like to find solutions to seemingly-unsolvable problems: any time we can find a workaround for something that looked impossible people just shake their heads and say, “Wow, those Scripting Guys are really amazing, aren’t they? I would have bet that there was no way to do X and yet they found a way to do it.”

So why don’t we like Microsoft Word anymore? Well, that’s easy: you never have to look for clever workarounds in Microsoft Word. Instead, pretty much everything you want to script in Microsoft Word can be done in a very simple, very straightforward fashion. And yes, that’s great for you, but it’s a real bummer for us; after all, no one’s going to say, “Wow, PS wanted to go to a specific line in a Word document and the Scripting Guys did that by using the GoTo method. They must be the smartest guys in the world!” Needless to say, it doesn’t take a genius to use the GoTo method to go to a specific line in a Word document.

Of course, on the bright side we didn’t have to put a whole lot of effort into answering this question either. (Scripting Guys are always torn between wanting to do something clever and being pretty dang lazy.) With that in mind, here’s a script that opens up a Word document (C:\Scripts\Test.doc) and then immediately moves the cursor to line 31:

Const wdGoToLine = 3
Const wdGoToAbsolute = 1

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

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

Set objSelection = objWord.Selection objSelection.GoTo wdGoToLine, wdGoToAbsolute, 31

We begin this script by defining a pair of constants: wdGoToLine (with a value of 3) and wdGoToAbsolute (with a value of 1). wdGoToLine tells the script what kind of object to go to; in addition to going to a line we could go to a bookmark, a paragraph, a page, a field, or all sorts of other things. (For a complete list, click here and look for the wdGoToItem enumeration.) wdGoToAbsolute tells the script to go to a specific line (we’ll tell it which line momentarily). Alternatively, we could go to the next line, the previous line, the first line, the last line, etc.

Next we create an instance of the Word.Application object and set the Visible property to True; this gives us a running instance of Microsoft Word that we can see onscreen. We use the Open method to open the document C:\Scripts\Test.doc, then use this line of code to create an instance of the Word Selection object:

Set objSelection = objWord.Selection

By default, the Selection object (and thus our cursor) will appear at the very beginning of the document. How do we move the cursor to, say, line 31? Here’s how:

objSelection.GoTo wdGoToLine, wdGoToAbsolute, 31

As you can see, all we do is call the GoTo method followed by our two constants and the number of the line we want to go to. That’s it; that’s all it takes.

Although that’s still a lot, you know; after all, we did have to look up the values for wdGoToLine and wdGoToAbsolute. And we did have to type the script into Notepad. That’s still pretty amazing, don’t you think?

Um, we said, “That’s still pretty amazing, don’t you think?”

See? That’s why we don’t like Microsoft Word anymore!

0 comments

Discussion is closed.

Feedback usabilla icon