none
Ascertaining the current cursor location RRS feed

  • Question

  • Hello,

    Do you know how to ascertain the current cursor location via code?

    If a range is selected, I know how to get the end point of the range.

    DimL_V_Scalar_Integer_SelectionEndLocation AsInteger= V_V_Scalar_Selection_Generic.Range.End

    But if nothing is selected, the value for L_V_Scalar_Integer_SelectionEndLocation above is 0. Instead, I would like it to be the current cursor location.

    Many thanks.

    Keith


    R K Howard

    Saturday, May 21, 2016 2:38 PM

Answers

  • If I run the macro from my last post against your test data, with 'text ' selected, it outputs:

    Normal Selection: test
    Main Text Story
    Start: 10, End: 15

    If you want just the start & end positions, they're returned by Selection.Start and Selection.End, respectively.

    Maybe your problem with V_V_Scalar_Selection_Generic.Range.End is in how that is defined and set.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 22, 2016 3:13 PM

All replies

  • If you want to test whether anything is selected and, if so, what the selection encompasses, use code like:

    Dim StrRpt As String
    With Selection
      Select Case .Type
        Case wdSelectionIP: StrRpt = "Insertion Point Selection"
        Case wdNoSelection: StrRpt = "No Selection"
        Case wdSelectionNormal: StrRpt = "Normal Selection: " & .Text
        Case wdSelectionBlock: StrRpt = "Block Selection"
        Case wdSelectionColumn: StrRpt = "Column Selection"
        Case wdSelectionRow: StrRpt = "Row Selection"
        Case wdSelectionInlineShape: StrRpt = "InlineShape Selection"
        Case wdSelectionShape: StrRpt = "Shape Selection"
        Case wdSelectionFrame: StrRpt = "Frame Selection"
      End Select
    End With
    MsgBox StrRpt

    With that information, you can then determine how the values returned by Selection.Start & Selection.End should be interpreted.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 22, 2016 6:50 AM
  • Hi Paul,

    Thanks for the information. Do you know how to identify the current cursor location if the selection mode is insertion point? As I noted, the value of V_V_Scalar_Selection_Generic.Range.End is being returned as 0 because there is no selection. There must be some other way to get the current character location within the document even if nothing is selected.

    Thanks.

    Keith


    R K Howard

    Sunday, May 22, 2016 11:30 AM
  • In that case, not only does the selection type = but also the start & end are the same. If that's 0, the implication is that the insertion point is at the start of the relevant story range. Thus:

    Dim StrRpt As String
    With Selection
      Select Case .Type
        Case wdSelectionIP: StrRpt = "Insertion Point Selection"
        Case wdNoSelection: StrRpt = "No Selection"
        Case wdSelectionNormal: StrRpt = "Normal Selection: " & .Text
        Case wdSelectionBlock: StrRpt = "Block Selection"
        Case wdSelectionColumn: StrRpt = "Column Selection"
        Case wdSelectionRow: StrRpt = "Row Selection"
        Case wdSelectionInlineShape: StrRpt = "InlineShape Selection"
        Case wdSelectionShape: StrRpt = "Shape Selection"
        Case wdSelectionFrame: StrRpt = "Frame Selection"
      End Select
      StrRpt = StrRpt & vbCr
      Select Case .StoryType
        Case wdMainTextStory: StrRpt = StrRpt & "Main Text Story"
        Case wdFootnotesStory: StrRpt = StrRpt & "Footnotes Story"
        Case wdEndnotesStory: StrRpt = StrRpt & "Endnotes Story"
        Case wdCommentsStory: StrRpt = StrRpt & "Comments Story"
        Case wdTextFrameStory: StrRpt = StrRpt & "TextFrame Story"
        Case wdEvenPagesHeaderStory: StrRpt = StrRpt & "Even Pages Header Story"
        Case wdPrimaryHeaderStory: StrRpt = StrRpt & "Primary Header Story"
        Case wdEvenPagesFooterStory: StrRpt = StrRpt & "Even Pages Footer Story"
        Case wdPrimaryFooterStory: StrRpt = StrRpt & "Primary Footer Story"
        Case wdFirstPageHeaderStory: StrRpt = StrRpt & "First Page Header Story"
        Case wdFirstPageFooterStory: StrRpt = StrRpt & "First Page Footer Story"
        Case wdFootnoteSeparatorStory: StrRpt = StrRpt & "Footnote Separator Story"
        Case wdFootnoteContinuationSeparatorStory: StrRpt = StrRpt & "Footnote Continuation Separator Story"
        Case wdFootnoteContinuationNoticeStory: StrRpt = StrRpt & "Footnote Continuation Notice Story"
        Case wdEndnoteSeparatorStory: StrRpt = StrRpt & "Endnote Separator Story"
        Case wdEndnoteContinuationSeparatorStory: StrRpt = StrRpt & "Endnote Continuation Separator Story"
        Case wdEndnoteContinuationNoticeStory: StrRpt = StrRpt & "Endnote Continuation Notice Story"
      End Select
     MsgBox StrRpt & vbCr & "Start: " & .Start & ", End: " & .End
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 22, 2016 12:07 PM
  • Hi Paul,

    I think I may not be communicating my point very well. Let me try again.

    Assume I have a document with the following content:

    This is a test to see if I can get the character location of the cursor.

    If the word ‘test’ is selected, and I execute the following line:

           Dim L_V_Scalar_Integer_SelectionEndLocation As Integer = V_V_Scalar_Selection_Generic.Range.End

    I think that L_V_Scalar_Integer_SelectionEndLocation will have a value of 15.

    However, if I position the cursor at location 15 (just after the ‘t’ in ‘Test’), without selecting anything (i.e., an insertion point cursor), I would like to run code to assign the value 15 to a variable. In this case, V_V_Scalar_Selection_Generic.Range.End is equal to 0. You stated “the implication is that the insertion point is at the start of the relevant story range”. However, that is not what I am finding. It looks like the .End value is equal to 0 even though the insertion point is at character location 15.

    Surely, there must be a one code line method of getting the current cursor location in a document??

    Do you know how to do that?

    Many thanks.

    Keith


    R K Howard

    Sunday, May 22, 2016 12:40 PM
  • If I run the macro from my last post against your test data, with 'text ' selected, it outputs:

    Normal Selection: test
    Main Text Story
    Start: 10, End: 15

    If you want just the start & end positions, they're returned by Selection.Start and Selection.End, respectively.

    Maybe your problem with V_V_Scalar_Selection_Generic.Range.End is in how that is defined and set.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 22, 2016 3:13 PM
  • Paul, If you run your code without test selected, but with an insertion point cursor positioned, say, just after the t in test, do you get 0 for Start and End?


    R K Howard

    Sunday, May 22, 2016 3:25 PM
  • In that case, I get:

    Insertion Point Selection
    Main Text Story
    Start: 11, End: 11

    Really, all this back & forth could be avoided if you ran the code for yourself...


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 22, 2016 10:26 PM
  • Hi Keith,

    I agree with Paul, Selection.End would return the value you want, and your issue might be related with  V_V_Scalar_Selection_Generic. Based on your code and description, I did not find how you set the value for it. After you leave cursor with an insertion point, I suggest you try this code to check the value.

    Debug.Print Selection.End
    

    If you still did not get the expected value, I suggest you share us the complete steps and code which could reproduce your issue.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 23, 2016 1:40 AM
    Moderator
  • Thanks everyone for your help. I have done extensive further testing. If something is selected, my code seems to work as expected, and the selection start and end range are where I expect them to be. However, something in my code is changing the selection location, but this only seems to be happening when the cursor is an insertion point and nothing is selected. This makes no sense to me, which is probably why my questions did not seem to make sense. When I run your code, it behaves as I expect, so the problem must be a bug in my code, which I need to resolve. Thanks again.

    R K Howard

    Thursday, May 26, 2016 4:00 PM
  • Hi R.Keith,

    I am glad you have narrow down this issue, if there is no other issues, I would suggest you mark the helpful reply as answer to close this thread. If you find the related code, and not able to modify the code to achieve your requirement, I would suggest you post a new thread, and share us the related code and steps to reproduce your issue.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 27, 2016 7:12 AM
    Moderator
  • Hello again.

    I finally found the problem.

    I was executing the following line somewhere in a preliminary procedure:

    V_V_Scalar_Application_Generic.Selection.EscapeKey()

    So, later on in the code, when my problematic code executed and tried to insert something at the current selection, the text was inserted at an ‘earlier’ location in the document than I expected – sometimes several hundred characters earlier.

    The problem only happened when the original selection was an insertion point. If the original selection had a span, the code worked fine.

    (I had originally thought the problem might be that the number of characters ‘earlier’ was the same as the size of the original selection, and that when the above EscapeKey executed, it put the insertion point to the start of the selection, and then, somehow, the insertion point got shifted back in the document by a number of characters equal to the size of the original selection. However, this explanation does not make sense, because, as I say, the code worked fine when the original selection had a span.)

    Anyway, the code works now after removing the nasty EscapeKey line above. If anyone, off the top of their head, has any clues, then great; otherwise, I just wanted to give you an update.

    Many thanks.

    Keith


    R K Howard

    Tuesday, June 7, 2016 10:28 AM
  • Hi R,

    I am glad your issue has been resolved and thanks for sharing solution, if you have any other issues related with Word developing, please feel free to post in this forum.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 8, 2016 5:16 AM
    Moderator