Scrolling the window so that start of selection in near the top of the window

C

Conan Kelly

Hello all,

I have many long docs that I need to scan through to delete unnecessary stuff. Is there a way that I can scroll the window so that
the start of the selection is near the top of the window?

Here is part of the code that I am currently using:

Do While Selection.Find.Execute
' Selection.Find.Execute
'Assigns the selection starting position to a variable then collapses the selection.
plngSelectionStart = Selection.Start
Selection.Collapse
'Finds and selects the end of the 2nd row of column headings then assigns its end position to a variable
Selection.Find.Execute " Rate Contact^p"
plngSelectionEnd = Selection.End
'Selects the page & column headers and deletes them
Selection.Start = plngSelectionStart
Selection.End = plngSelectionEnd
ActiveWindow.SmallScroll -10
pmbrDeleteResponse = MsgBox("Do you want to delete this selection?", vbYesNoCancel + vbQuestion, "Delete Selection")
If pmbrDeleteResponse = vbYes Then
Selection.Delete
ElseIf pmbrDeleteResponse = vbCancel Then
Exit Sub
Else
Selection.Collapse 0
End If
'Resets the text to find back to the search string variable
'Otherwise the DO WHILE loop would continue to look for the end of the 2nd line of column headings
Selection.Find.Text = pstrSearchText
Loop


I tried using "ActiveWindow.SmallScroll -10" to scroll the window down 10 lines after it selects the text I'm looking for.

The problem is sometimes the selection can be 50 lines long and after the SmallScroll, I can only see the bottom 10 line. Or
sometime the very next block of text to find is already visible on the screen, and after it is selected, SmallScroll may kick it off
the bottom of the screen.

How can I edit this code to get the start of the selection to be scrolled to 5 lines down from the top of the window?
 
A

Andrew V

Use the feature that, if a selection is extended via movement keys, the
scroll location will change to show the point of change.

Dim myRange1, myRange2 As Range
Set myRange1 = Selection.Range 'Full result
Selection.Collapse
Set myRange2 = Selection.Range 'Starting point of result
Selection.EndKey unit:=wdStory
myRange2.Select
Selection.MoveRight unit:=wdCharacter, Extend:=wdExtend
myRange1.Select

---

Alternatively, use Selection.Range.ComputeStatistics(wdStatisticLines).
With this code, you can determine how many lines the selection spans (or for
pages, use wdStatisticPages). Then you can use this value to determine how
much you should scroll.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top