none
Scroll Word document programatically RRS feed

  • Question

  • Hello all,

    I'm selecting some Range in Word document by setting Bold property. Sometimes, this range locates on other page then has been shown to user. So, I need to scroll the document to corresponding place in relevant page. But I've no idea how can I do this...

    Any help will be appreciated,

    Thanks ahead

    Thursday, April 1, 2010 3:24 PM

Answers

  • Hello VandG,

    Welcome to VSTO forum!

    As my understand, you are going to programmatically display a specific page on user's screen within a multi-page Word document, right?

    In Word Object Model, we have a Selection object that present the content that user are currently selected by the cursor, and it is the displaying part of the whole document, so, we could make use of Selection to achieve your requirement. The way we set a Selection is by using the Range.Select method (http://msdn.microsoft.com/en-us/library/bb217068.aspx), please refer to following code:

    Application.ActiveDocument.Paragraphs(2).Range.Select 
     

    Above code will bring me to the second page if I've created a blank document and input several ctrl+Enter in it.

    Hope this helps.

    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by VandG Friday, April 2, 2010 9:13 AM
    Friday, April 2, 2010 3:38 AM

All replies

  • Hello VandG,

    Welcome to VSTO forum!

    As my understand, you are going to programmatically display a specific page on user's screen within a multi-page Word document, right?

    In Word Object Model, we have a Selection object that present the content that user are currently selected by the cursor, and it is the displaying part of the whole document, so, we could make use of Selection to achieve your requirement. The way we set a Selection is by using the Range.Select method (http://msdn.microsoft.com/en-us/library/bb217068.aspx), please refer to following code:

    Application.ActiveDocument.Paragraphs(2).Range.Select 
     

    Above code will bring me to the second page if I've created a blank document and input several ctrl+Enter in it.

    Hope this helps.

    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by VandG Friday, April 2, 2010 9:13 AM
    Friday, April 2, 2010 3:38 AM
  • Dear Tim,

    Thank you for this great tip. It's working!

    One more question, please:
    Scrolling the document by calling Range.Select() mehtod brings the wishful text to the bottom of the screen. Does it possible somehow to bring this text to the top of the page or some desired page position?

    I tried somethig like this:

    Object endOfDoc = @"\endofdoc";
    range.Document.ActiveWindow.VerticalPercentScrolled = range.End * 100 / range.Document.Bookmarks.get_Item(ref endOfDoc).Range.End;

    but it doesn't look like working - it brings desired text too high on the page. I think that I can't prepare this calculation referencing Range.End positions.

    Thank you in advance,
    V&G

    Friday, April 2, 2010 9:41 AM
  • Hi V&G

    I have some new findings, please check following line:

    ActiveWindow.ScrollIntoView Selection.Range, True

    Append above code to my last reply would do the trick.

    Thanks.
     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, April 6, 2010 8:38 AM
  • Thank you for your help!

    I'm not sure I understanded you correctly. Do you have in mind the following:

    Object selectedRangeStart = true;
    selectedRange.Document.ActiveWindow.ScrollIntoView(selectedRange,
    ref selectedRangeStart);

    I also tried to use a selected range start position as an initialization for second parameter - it didn't work for me...

    Best regards,
    V&G

    Tuesday, April 6, 2010 3:47 PM
  • Hello V&G,

    Sorry for the dely, your result above is right, it only works when the selection is not showing at current screen. To make the selection out of current screen we could set ActiveWindow.VerticalPercentScrolled=100 to scroll to the bottom of the screen. and this is not a perfect solution but it will work fine in the most case.

    Hope this helps.

    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 8, 2010 6:40 AM