{"id":68963,"date":"2005-09-14T13:50:00","date_gmt":"2005-09-14T13:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/14\/how-can-i-open-word-with-the-cursor-positioned-at-the-start-of-a-specified-line\/"},"modified":"2005-09-14T13:50:00","modified_gmt":"2005-09-14T13:50:00","slug":"how-can-i-open-word-with-the-cursor-positioned-at-the-start-of-a-specified-line","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-open-word-with-the-cursor-positioned-at-the-start-of-a-specified-line\/","title":{"rendered":"How Can I Open Word with the Cursor Positioned at the Start of a Specified Line?"},"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 open Word with the cursor positioned at the start of a specified line?<BR><BR>&#8212; PS<\/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, PS. You know, we Scripting Guys have decided that we don\u2019t 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, \u201cWow, those Scripting Guys are really amazing, aren\u2019t they? I would have bet that there was no way to do <I>X<\/I> and yet they found a way to do it.\u201d <\/P>\n<P>So why don\u2019t we like Microsoft Word anymore? Well, that\u2019s 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\u2019s great for you, but it\u2019s a real bummer for us; after all, no one\u2019s going to say, \u201cWow, 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!\u201d Needless to say, it doesn\u2019t take a genius to use the GoTo method to go to a specific line in a Word document.<\/P>\n<P>Of course, on the bright side we didn\u2019t 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\u2019s a script that opens up a Word document (C:\\Scripts\\Test.doc) and then immediately moves the cursor to line 31:<\/P><PRE class=\"codeSample\">Const wdGoToLine = 3\nConst wdGoToAbsolute = 1<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;,,TRUE)<\/p>\n<p>Set objSelection = objWord.Selection\nobjSelection.GoTo wdGoToLine, wdGoToAbsolute, 31\n<\/PRE>\n<P>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 <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbawd11\/html\/wohowConstants_HV01049731.asp\" target=\"_blank\"><B>here<\/B><\/A> and look for the wdGoToItem enumeration.) wdGoToAbsolute tells the script to go to a specific line (we\u2019ll tell it <I>which<\/I> line momentarily). Alternatively, we could go to the next line, the previous line, the first line, the last line, etc.<\/P>\n<P>Next we create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> property to True; this gives us a running instance of Microsoft Word that we can see onscreen. We use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.doc, then use this line of code to create an instance of the Word <B>Selection<\/B> object:<\/P><PRE class=\"codeSample\">Set objSelection = objWord.Selection\n<\/PRE>\n<P>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\u2019s how:<\/P><PRE class=\"codeSample\">objSelection.GoTo wdGoToLine, wdGoToAbsolute, 31\n<\/PRE>\n<P>As you can see, all we do is call the <B>GoTo<\/B> method followed by our two constants and the number of the line we want to go to. That\u2019s it; that\u2019s all it takes.<\/P>\n<P>Although that\u2019s still a lot, you know; after all, we <I>did<\/I> have to look up the values for wdGoToLine and wdGoToAbsolute. And we <I>did<\/I> have to type the script into Notepad. That\u2019s still pretty amazing, don\u2019t you think? <\/P>\n<P>Um, we said, \u201cThat\u2019s still pretty amazing, don\u2019t you think?\u201d <\/P>\n<P>See? <I>That\u2019s<\/I> why we don\u2019t like Microsoft Word anymore!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I open Word with the cursor positioned at the start of a specified line?&#8212; PS Hey, PS. You know, we Scripting Guys have decided that we don\u2019t like Microsoft Word anymore. How come? Well, as Scripting Guys we like to find solutions to seemingly-unsolvable problems: any time we can find [&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-68963","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 open Word with the cursor positioned at the start of a specified line?&#8212; PS Hey, PS. You know, we Scripting Guys have decided that we don\u2019t like Microsoft Word anymore. How come? Well, as Scripting Guys we like to find solutions to seemingly-unsolvable problems: any time we can find [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68963","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=68963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68963\/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=68963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}