Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting cursor in word doc 4

Status
Not open for further replies.

shannanl

IS-IT--Management
Apr 24, 2003
1,071
0
0
US
How do I programatically (from VB application) set the cursor to be in a specific location when I open up a word doc?

Thanks,

Shannan
 
Something like this should work, you create a bookmark wherever you want the cursor to be and add macro to your doc template be it normal.dot or whatever and get it to run when you open a document.
Code:
Sub FocusField()
ActiveDocument.Bookmarks("Text35Orig").Range.Fields(1).Result.Select
End Sub
 
If you know the specific location, then simply make the cursor start there. Everything in Word starts counting from the beginning of the document (0), in character spaces.

The cursor in Word is called the Selection, and it has a range start (and end). These are stored as Long integers.

So if you know the specific location (say 14672) then to make the cursor go there:

Selection.Start = 14672

Most people do not know the specific location as an integer. The issue is - do you know the specific location as a place? And do you just want to go there? Or do you want to select anything on the way. For example:

' just in case the selection is larger than one character
Selection.Collapse Direction:=wdCollapseStart
Selection.Start = ActiveDocument.Paragraphs(3).Start

If the Selection (cursor) is AFTER the start of paragraph 3, everything from the Selection point to the start of paragraph 3 will be selected (highlighted). It will select backwards.

If the selection is BEFORE the start of paragraph 3, then the Selection (cursor) will simply move to the start of paragraph 3.

Do you want to select anything between where you are, and the "specific location"? This is different from just going to that location.

Yachtie's suggestion of using a bookmark is one of the best ways. Although the code is not the way I would do it.

Selection.GoTo What:=wdGoToBookmark, Name:="Text35Orig"

selects the bookmark. No need for Field, or Result.

Here is another example, I always have the following code in my documents. I have this in my ThisDocument module. What it does is simple. Whenever the document closes, it places a bookmark, named "StoppedHere", at the last location of the cursor. Whenever the document opens, it looks for that bookmark, and goes there. It then deletes it, leaving the way for making a new one, when I close the document. This way, I always come back to to where ever I was.

yes, I know Shift-F5 returns to the last location in a document - but it does NOT work on document open.

Code:
Sub Document_Close()
If ActiveDocument.Bookmarks.Exists("StoppedHere") = False Then
    ActiveDocument.Bookmarks.Add _
        Name:="StoppedHere", Range:=Selection.Range
End If
End Sub
'

Sub Document_Open()
If ActiveDocument.Bookmarks.Exists("StoppedHere") = True Then
    Selection.GoTo what:=wdGoToBookmark, Name:="StoppedHere"
    ActiveDocument.Bookmarks("StoppedHere").Delete
End If
End Sub

So, what specific location? Oh, BTW: here is a way to find out the specific location of the cursor.

Code:
Sub Here()
Dim X As Long
Selection.Collapse Direction:=wdCollapseStart
X = Selection.Start
Msgbox "Cursor is at character space " & X & _
   ".  Start of document = 0."
End Sub

I bet you will be surprised at the numbers that may come up.

Gerry
 
Gerry,

I tried your (Selection.Start = 14672) and it worked perfectly. I mean the number you had in there was EXACTLY where I needed the cursor to start at!! I was going to start with 14672 and modify it from there but it was perfect!

Thanks all of you for the info.

Shannan
 
You have to be kidding, right? I put that number in randomly.

Gerry
 
No, Im not kidding.

Shannan
 
fumei,

Thanks for the info - it deserves another STAR.

And based on your "6th sense" in getting the exact number of 14672, you should:
a) Get some kind of BONUS, and
b) Go out and purchase a lottery ticket. ;-)

Regards, ...Dale Watson dalwatson@gov.mb.ca
 
I am having a very hard time believing this. Maybe I should get a lottery ticket! I never buy them...but yikes, what are the odds???

Or is my leg being pulled here?

Actually it was amusing (a while ago) when I realized why the absolute location had to be a Long integer. I had been using just an Integer. Then on a fairly long (pun intended) document I kept on getting out of range errors. Come on...really? That was when I realized that Word counts everything (and I mean everything) as a sequence number from 0 (doc start) by character spaces. Doh! Hmmmm. Location = 347,901...just a wee bit out of range of an Integer.

Just as an aside. This is also how you numerically expand the text contained within a bookmark. If you go to a bookmark containing text, and add text - the added text will NOT be contained in the bookmark. Even if you explicitly state Bookmark("BlahBlah").Range.Text = strNewText.

That adds the text to the existing text but OUTSIDE the bookmark.
You must resize the .End location to include the new text. as in:

Bookmark("BlahBlah").Range.Text = strNewText
Bookmark("BlahBlah").End = (Bookmark("BlahBlah").End + _
Len(strNewText))


Gerry
 
fumei
PurplePointyPip4u!
Just the sort of thing people like me need to try and get a grasp of the basics of Word VBA! Did you ever do that training course you were on about a few months back?

Re Dale's second point, would you pick me 6 numbers between 1 and 49 if you don't mind!!

Dale!! Where u bin??

Happy Friday
;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Yeah I have a few dollars I would like to go in on that one with you guys!
 
RE: my Advanced Word and VBA course. I am almost finished writing it. I am going to do a test run on some of the modules today - fortunately in an empty classroom, so no one will hear it when I flub things. I hope to start being able to deliver the course the end of September.



Gerry
 
Hi Loomah, just to show that we all keep learning, I had a push from an Excel question (although it was regarding ActiveX checkboxes) in thread707-910901.

I just discovered how to detect what type of ActiveX controls are in a Word document - which, of course, is rather different than Excel.

Strange world, out there in MS-land.

Gerry
 
Just as a final input to this thread. I NEVER buy lottery tickets. I have a reasonably mathematical mind, and I undertand the concept of odds. However, I spent a dollar, just because this was so weird (if indeed true that my random location number was exactly correct).

Anyway, I spent my one dollar.....and got $405 out of it! Hey not enough to retire on, but sort cool. Last lottery ticket I bought must be 15 years ago.

Strange.

Gerry
 
I would buy 405 tickets with the way your luck it going!!!

Shannan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top