none
Get the page number of the cursor in Word document RRS feed

  • Question

  • Hi,

    I am using below code to get the cursor location in word document and to move the control to the cursor location.

    int PageNum = Document.ActiveWindow.Selection.get_Information(WdInformation.wdActiveEndPageNumber);

     Object pgnum = PageNum;
     aSel.GoTo(ref aWhat, ref aWhich, ref pgnum, ref ourMissing);

    In above code PageNum value was always last page number of the document but using GOTO method and with the value of PageNum I was able to move the control to to the cursor location in word 2007 and 2010. But this was not possible in word 2013.

    1. Is this proper way to get the cursor location ? If not How can I get page number of the        document where cursor is present. 

    2. Is GOTO() method which I have used in above code is proper to move the control to the cursor location ?

    Or is there any other way how we can move the control to the cursor position of the document ?

    Thanks,

    Gayatri





    • Edited by GayatriK Tuesday, April 3, 2018 2:56 PM
    Tuesday, April 3, 2018 10:56 AM

All replies

  • Hello Gayatrik,

    >>I was able to move the control to to the cursor location 

    What do you mean moving control to the cursor location? You could only go to the page where the cursor location is instead of the cursor location itself via the page number. 

    >>But this was not possible in word 2013.

    What do you mean it is not possible in word 2013? Did you get error PageNum or GoTo function failed to move control to the cursor location?

    >>aSel.GoTo(ref aWhat, ref aWhich, ref pgnum, ref ourMissing);

    What's aSel, aWhat, aWhich? How did you set them?

    Best Regards,

    Terry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 4, 2018 5:42 AM
  • I didnt get any error when I was trying to get the cursor page number. But instead page Number value was always the last page number of the document. 

    I want to get the page number of the document where the cursor will be there. Once I get proper value of page number where cursor is present with the help of GOTO() I can make cursor to go back to its previous position that is previous page.

      Or Can you suggest me how i can make cursor to back to the previous position or to the previous page ?
    • Edited by GayatriK Wednesday, April 4, 2018 7:01 AM
    Wednesday, April 4, 2018 6:47 AM
  • Hello Gayatrik,

    What do you mean back to previous positition or previous page? If the page number is 3,where would you move the cursor?

    What application are you developing? What's aSel, aWhat, aWhich? Could you please share a simply project and detail the your rest steps so we could try to reproduce your issue?

    For sharing file, you could share the file via Cloud Storage, such as One Drive, and then put link address here. Thanks for understanding.

    Best Regards,

    Terry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 4, 2018 8:11 AM
  • Hi,

    Actually in my application we are adding bookmarks from starting to ending of the document.To insert bookmarks we use below code.

    object aWhat = WdGoToItem.wdGoToPage,
        aWhich = WdGoToDirection.wdGoToAbsolute;
    Object iObj;
    Selection aSel = Document.ActiveWindow.Selection;

    in for loop 
    {
    Bookmarks.Add("Bookmark_Name", aSel.GoTo(ref aWhat, ref aWhich, ref iObj, ref ourMissing));

    //Note: loop may repeate for any number of times depending on the bookmarks we want to insert and it is contious starting from page 1 to end page of document.

    int PageNum = aSel.get_Information(WdInformation.wdActiveEndPageNumber);   
    Object pgnum = PageNum;
    aSel.GoTo(ref aWhat, ref aWhich, ref pgnum, ref ourMissing);  


    because of adding bookmarks with above piece of code cursor will reach at the end of document as we will insert bookmarks till last page of document. I want to retain the cursor at the same position where it was atleast in the same page. 

    So i am trying to get cursor location or cursor page number so that I can use same GOTO() method to make cursor to go back to its position/to its page once bookmark insertion will complete.

    Thanks,
    Gayatri
    Wednesday, April 4, 2018 11:14 AM
  • Hello Gayatrik,

    So what you want is moving your cursor to add bookmark and then back your cursor to cursor original position before moving cursor?

    I would suggest you use a range object to record the position/range and re-select the range at last.

                //record current cursor position
                Word.Range range = document.ActiveWindow.Selection.Range;
                //do your operation here
                ....
                //back to recorded cursor psotion
                range.Select();

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 6, 2018 9:13 AM
  • Hello,

    Has your original issue been resolved? If it has, I would suggest you mark the helpful reply as answer or provide your solution and mark as answer to close this thread. If not, please feel free to let us know your current issue.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 9, 2018 6:17 AM
  • Hi 

    I was not able to find the exact location of the cursor but it is possible to find the page number where the cursor will be there. I have used below code to get the cursor page number.

     Selection aSel = WordDocument.ActiveWindow.Selection;
    int PageNum = aSel.get_Information(WdInformation.wdActiveEndPageNumber);  

    PageNum will give exact page number where cursor will be there.

    and to go back to the cursor page use the above pageNum and sSel.GoTO() method(https://docs.microsoft.com/en-us/office/vba/api/word.selection.goto)

    Thanks,

    Gayatri

    Tuesday, September 4, 2018 1:43 PM