none
Word - Cursor Position

    Question

  • Hi,

    I'm trying to get the cursor position after the last typed character in a line, using the code:

    Range range1 = this.Application.Selection.Range;

    but the returned Range has wrong values!

    for example:

    Text:
    aa|        (pipe is the cursor)

    range1.Start -> 130
    range1.End -> 130

    I think the problem is that the document need to be update. But how can i do that?

    Bye,
    Thank you,
    Pedro Rocha
     




    Friday, June 16, 2006 5:34 PM

Answers

  • <<i'm using the method Range.Calculate() to refresh the range value, and it's working, now the value is correct!>>

    That's good to know :-) But in answer to your question yes, you can certainly get the last word before the cursor. The question, in this case, is whether it's better to work with the Range or the Selection object. The Range object orients itself towards what was just typed; the Selection object more towards what is just after the cursor.

    Assuming that the user has not yet typed a space, this

      application.Selection.Range.Words(1)


    will return the word that was just typed. (The Word object here returns a Range object, BTW)

    If punctuation has been typed (such as  a comma) then the above returns the punctuation symbol. If a space has been typed, then it returns what FOLLOWS (the next word, which could be a paragraph mark). If this is what you anticipate, then you need to move the range back (VBA-speak; just to give you an idea how you can work with moving the range):

    Sub GetLastWord()
        Dim rng As Word.Range
       
        Set rng = Selection.Range
        Debug.Print app.Selection.Text, rng.Words(1)
        Select Case rng.Characters(1)
            Case " "
                rng.MoveEnd Unit:=wdCharacter, Count:=-1
            Case ","
                rng.MoveEnd Unit:=wdCharacter, Count:=-2
            Case Else
        End Select
        Debug.Print rng.Text, rng.Words(1)
    End Sub

    Monday, June 19, 2006 4:20 PM
    Moderator

All replies

  • Hi Pedro

    Why do you think the range has the wrong values? Start and End should be the same, since the current selection is a "point" (blinking).

    You think 130 is too large a value? Too small? What is in your document? Any fields? Hidden text?

    Why do you need the Start and End values? Generally, it's a bad idea to rely on these for anything but comparison purposes. Usually, there's another, more reliable approach to accomplish most things one needs to do with a range.

    Friday, June 16, 2006 6:51 PM
    Moderator
  •  

    My document has no fields or hidden text, that's only these two characteres.

    I think 130 is too large and wrong because when i move the cursor to another position and back to the last line position or when i simulate a keyboard event (like a SHIFT or CTRL) the range.end gets the value 3 (in my opinion the correct value).

    I need the value because i'm implementing a "Intellisense" system in word, and i need the cursor position after a keypressed event to know what word my system has to complete. Now i'm simulating a the keyboard event, but this is limitting my solution.

     

    thanks for all,

    bye

     

    Friday, June 16, 2006 7:21 PM
  • Yes, 130 does seem a bit... over the top. I did a quick test yesterday, before answering you - at startup move the cursor to the end of the document, then get the Start/End values - because I was wondering what all VSTO might add into the document range. It returned 4, which is more reasonable than 130 :-) but I would have expected 3.

    However, I still don't understand why you need these values. Once you have the Range object, itself, how many characters it is from the beginning of the document really is irrelevant. How are you trying to implement the "intellisense", more exactly, in relation to the range (position in a document)? (Especially since Word doesn't expose any "key" events...)

    Saturday, June 17, 2006 9:37 AM
    Moderator
  • Hi Cindy,

    I need this cursor position to get the last word before the cursor, the word that will be completed. When i get a keyboard event (i implement a library for this) i get the last word before the actual cursor position and i show a listbox with the possibilities...

    Is possible refresh the ranges values??

    Thank you,
    Bye


    Monday, June 19, 2006 8:46 AM
  • Hi,

    i'm using the method Range.Calculate() to refresh the range value, and it's working, now the value is correct!
    Monday, June 19, 2006 1:36 PM
  • <<i'm using the method Range.Calculate() to refresh the range value, and it's working, now the value is correct!>>

    That's good to know :-) But in answer to your question yes, you can certainly get the last word before the cursor. The question, in this case, is whether it's better to work with the Range or the Selection object. The Range object orients itself towards what was just typed; the Selection object more towards what is just after the cursor.

    Assuming that the user has not yet typed a space, this

      application.Selection.Range.Words(1)


    will return the word that was just typed. (The Word object here returns a Range object, BTW)

    If punctuation has been typed (such as  a comma) then the above returns the punctuation symbol. If a space has been typed, then it returns what FOLLOWS (the next word, which could be a paragraph mark). If this is what you anticipate, then you need to move the range back (VBA-speak; just to give you an idea how you can work with moving the range):

    Sub GetLastWord()
        Dim rng As Word.Range
       
        Set rng = Selection.Range
        Debug.Print app.Selection.Text, rng.Words(1)
        Select Case rng.Characters(1)
            Case " "
                rng.MoveEnd Unit:=wdCharacter, Count:=-1
            Case ","
                rng.MoveEnd Unit:=wdCharacter, Count:=-2
            Case Else
        End Select
        Debug.Print rng.Text, rng.Words(1)
    End Sub

    Monday, June 19, 2006 4:20 PM
    Moderator
  • Thank you very much Cindy!!


    Bye,
    Pedro
    Monday, June 19, 2006 5:40 PM
  • Cindy:

    What if there is a usercontrol (e.g. textbox) that is moved somewhere past the last text in a document?  How do you determine where the real end of the document is?  For example, if you press Ctrl, Shift, End, it will highlight all of the text in a document, but it will not include the user control if it is past the last word in the document.  I am trying to use VSTO to add some text at the complete end of the document, which may or may not include usercontrols, images, etc.  Thanks.

     

    Roman

    Wednesday, September 19, 2007 10:07 PM
  • you can use

     

    use

    Selec.Sentences.Item(1).Text

    to get the last type word and put this in your key press code

     

     

    Thursday, March 20, 2008 11:17 AM