Link to home
Start Free TrialLog in
Avatar of npaun
npaun

asked on

How to scroll end of Word document close to the toolbar bottom?

Well, how to scroll a selection/range close or at the end of the last page in a document, so that the selection/range is close to the top of the active area, i.e. below the bottom of the toolbar? Example can be seen in picture, which is a situation when spellchecker finds unknown word at the end of document and scrolls document so the word is at top.

All methods I’ve tried up to know, only manage to scroll the document so that the bottom of the last page is close down to the status bar…

I attached a picture as an example what I mean by this...
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

I have taken a look through various settings and can't see a User option for this function. There is a big list of layout options at the bottom of the Advanced tab in Options but none of these appear to give this as an option.

I assume it goes the way it does with the Spell Check window so that the user can see the full context of the text that the Spell Check is querying. At the end of a document, there would be a couple of options:

1) Push the Spell Check window to one side but there is still a risk that it could be covering a pertinent piece of text for the user to decide on spelling,
2) Push the document up as it does.

The fact that it does it with the Spell Check window suggests that it is possible but I suspect you would have to dig into the GUI script or write a VBA routine that forces the scroll position beyond the normal maximum.

Can I ask why you want to do this?

Thanks
Rob H
Just recorded a VBA Routine to scroll to bottom of the page and got:

Sub Scroll()
    ActiveWindow.ActivePane.VerticalPercentScrolled = 62
End Sub

I can take the value at the end up to 100 but over 100 gives a Runtime error: Value out of range.
Avatar of npaun
npaun

ASKER

-I probably should additionally clarify my question: I meant, how to do such a scrolling programmatically, by using VBA or VB6...
-Well, I want this from a similar reason as it is used in Word spellchecker. I 'm writing a Word add-in which does some inspection of the text, and at some point it should select a word in the text... and from same reason as for the spellchecker, it is convenient that the selection is located at the top of window... this can be done for most pages, but for the last one it is a problem: for it, the bottom of the page must be scrolled upward, in the way I described
-I tested ActiveWindow.ActivePane.VerticalPercentScrolled and it cannot perform whit it takes, it can only scroll normally to the end of page, i.e. the end of page would be at the bottom, not close to the top...
ASKER CERTIFIED SOLUTION
Avatar of Vadim Rapp
Vadim Rapp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of npaun

ASKER

@vadimrapp1

Thank you, this looks very promising. The Selection.find looks particularly promising for my purposes. However, I have some problems in implementation. Based on  http://msdn.microsoft.com/en-us/library/office/ff839325%28v=office.15%29.aspx
I’m using the following test code

With Selection.Find
 .Forward = False
 .ClearFormatting
 .MatchWholeWord = True
 .MatchCase = False
 .Wrap = wdFindContinue
 .Execute FindText:="Microsoft"
End With

It works absolutely perfectly, if the particular word, in this example "Microsoft", is out of view, and after calling this code, the word is positioned right below the toolbar, and even if the word is at the end of the last page, just PERFECT! But, if the word (e.g. "Microsoft") is visible on the screen, then the word is just highlighted, and the page is not scrolled. I’ve been testing, and tried to work around in this way: before calling the previous code, I would scroll document for some amount so that the word goes out of view, and then calling the code, and that achieves the effect. But, to do this, I must use ScreenUpdating=False/True, and this prevents “document jumping”, but unfortunately, often, in ~10-30% of callings, use of this ScreenUpdating=False/True seems to produce some kind of a flickering, when fast black stripes shortly appear at sides of the document, which looks ugly and spoils the workaround...

Do you have some advice regarding this?
I’m using the following test code. Select a word in the text, and call the code. The document is scrolled so that the word is highlighted and positioned below the toolbar, and even if it is at the end of the last page. But, the flickering occurs sometimes. Using Word 2010.

Private Sub Command1_Click()
    Dim MyRange As Range
   
    On Error Resume Next
   
    Set MyRange = WrdDoc.Range
    Set MyRange = WrdApl.Selection.Range
   
    WrdApl.Application.ScreenUpdating = False
   
'    WrdApl.Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=100
    WrdApl.ActiveWindow.PageScroll True
    WrdApl.ActiveWindow.PageScroll True
   
    WrdApl.Selection.Start = MyRange.Start
    WrdApl.Selection.End = MyRange.Start

    With WrdApl.Selection.Find
        .Forward = True
        .ClearFormatting
        .MatchWholeWord = True
        .MatchCase = False
        .Wrap = wdFindContinue
        .Execute FindText:=MyRange.Text
    End With
   
    WrdApl.Application.ScreenUpdating = True
End Sub
I would try pretty much the same, i.e. to try to scroll back and forth etc. If it flickers, well...

But, I have a revolutionary suggestion. When Microsoft developers were making their decision, how Word should react when it finds the text, and they decided that if the target is already on the screen, it's enough to highlight it, rather than scroll, maybe they had a reason to think that this is in fact optimal¹? And also, if you look at other applications, for instance the web browser you are looking at, you will see that they behave in the same way: right now when you read it, press ctrl-F, search for this very phrase, and you will see that the browser behaves in the same way as Word. Any browser. So maybe, for the purposes of consistent user experience, it's better to leave it the way it has been for the last 20 years.

¹) Microsoft spends very serious money on all kinds of grants to the universities on the research of UI. Always did. So it's possible that the decision not to scroll when it's on the page was in fact the result of a study by someone with world-known name, 40 years ago.
Avatar of npaun

ASKER

Well, the problem “How to scroll end of Word document close to the toolbar bottom” is actually a part (and a side effect) of a larger problem I must solve... What I actually need is how to scroll a range/selection so it is always shown a the same place in the Word i.e. below the toolbar. I have found a way how to approximately do that, but it doesn’t work when a range/selection is close to the end of the last page, hence my current question. The current approach does help for scrolling, but on the other hand has its own drawbacks which probably make it a half-solution...
You are right when you say, one should stick to a widely accepted interface behaviors. The behavior of the search function is quite logical, and should be adhered, I agree. But, I don’t need a search behavior; I need a way to scroll a selection so that the selection is positioned always at approximately the same place, below the toolbar, always, regardless to where the selection is, beginning, middle or end of page... When you use Word spellchecker, you’ll notice that when an unknown word is found, it is selected and the document is scrolled so that selected word is located approx. three rows bellow the toolbar, and that is exactly what I need to replicate.. And the Find method is just a method you’ve suggested, and it works only partially, for my purposes, as a workaround; so if I don’t  have a complete solution for my problem, I have to find a way to amend your suggested solution...
Here's one way to do it:  
Application.ScreenUpdating = False
    s = "the text to look for"

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = s
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    
    Dim x As Long, pTop1 As Long, pTop2 As Long
    ActiveWindow.GetPoint x, pTop1, x, x, Selection.Range

    Const WantedSelectionPosition = 200 ' <---- specify where you want the selection
    
    If pTop1 > WantedSelectionPosition Then
        ActiveWindow.SmallScroll 1 ' experiment to find out movement per 1 smallscroll
        ActiveWindow.GetPoint x, pTop2, x, x, Selection.Range
        If pTop2 > WantedSelectionPosition Then
            decrement = Abs(pTop2 - pTop1)              ' movement achieved by one SmallScroll
            ActiveWindow.SmallScroll CInt((pTop2 - WantedSelectionPosition) / decrement)
        End If
    End If
    
    Application.ScreenUpdating = True

Open in new window


The above code calculates the needed scroll by making one "experimental" smallscroll and seeing by how much the screen moved, assuming that it may depend on monitor resolution etc. Chances are, this is not needed, and it always moves by 10, then you can do it without "the experiment", in one operation.
Avatar of npaun

ASKER

Thanks! I will see how to best combine these suggestions...

For the end, do you know why Selection.GoTo can only scroll page only to some extent, and no further? For instance, if a page is filed with some text, the line

WrdApl.Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Count:=1

can only scroll page until 8 rows of text (12pt Times New Roman) are visible bellow the toolbar, and cannot scroll the  last row closer to the toolbar... Is there a way to scroll a page more (by using Selection.GoTo) so that only one or two rows bellow the toolbar are visible?
> Is there a way to scroll a page mor

You probably can do it by using the same activewindow.smallscroll as in my code.
Avatar of npaun

ASKER

Unfortunately not: when the the page is scrolled by GoTo so that the end of document is above the status bar, the scroll bars (and hence smallscroll ) cannot scroll the page more in the Up direction...
I've requested that this question be closed as follows:

Accepted answer: 500 points for vadimrapp1's comment #a40370547

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.