Scroll a Word Document using VBA (1 Viewer)

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:51
Joined
Feb 28, 2001
Messages
27,313
Scenario: A word document contains some things that I might need to reference from time to time. As a "help" feature I have a way to open that document from Access. (This part works.) I have bookmarks all through the document that represent the things that might need to be referenced. So I try to go to the bookmark. (This part also works.)

What doesn't work is that the bookmark comes out at the bottom of the screen in which the Word document opens. I wanted it at the top.

Code:
Public Sub OpenHow2Help(oWd As Word.Application, sBkMk As String)

Dim sBEFS As String                           'file spec of the back-end file
Dim bGotIt As Boolean                        'slot for return of the OPEN call
Dim wdActive As Word.Document         'selector for the word document
Dim vBkMk As Variant                        'word, inexplicably, likes variants

    vBkMk = sBkMk
    sBEFS = WhereIsTable("tHLPREFS")        'get location of the back end file
    
    sWrdHow2File = FindFileDev(sBEFS) & FindFileDir(sBEFS) & "\HOW2.DOCX"
    
    OpenWordRO oWd, sWrdHow2File, bGotIt    'try to open the object
    
    If bGotIt Then
        oWd.Documents(sWrdHow2File).Activate
        If ActiveDocument.Bookmarks.Exists(sBkMk) = True Then
            ActiveDocument.Bookmarks(vBkMk).Select
            ActiveDocument.GoTo What:=wdGoToBookmark, Name:=vBkMk
            ActiveDocument.Windows(1).ScrollIntoView ActiveDocument.Bookmarks(sBkMk), True
        End If
    End If

End Sub

The above sample fails on the .ScrollIntoView part. Everything else works. Has anyone else used something like this before? The goal is to bring the bookmark to the top left of the active window. I've tried other variants of this puppy, this is just the latest iteration.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:51
Joined
Feb 28, 2001
Messages
27,313
After playing with it for entirely too long, I found an answer of sorts. It is ugly by any standards, but it works.

Code:
            oWd.ActiveDocument.GoTo What:=wdGoToBookmark, Name:=vBkMk   'go find it
            oWd.ActiveDocument.ActiveWindow.LargeScroll down:=1         'scroll one frame (but it overshoots)
            oWd.ActiveDocument.ActiveWindow.SmallScroll up:=5           'scroll back a few lines to correct overshoot

Go to the bookmark. Scroll down one frame (Large Scroll call). Scroll up five lines (Small Scroll call). The text of the item at the start of the bookmark is now within five lines (or less) from the top of the window.
 
Local time
Today, 04:51
Joined
Feb 28, 2023
Messages
630
I know this is an old thread, but I had the same question and @The_Doc_Man has helped me out before, so I wanted to give back a little bit.

Basically, the answer is a combination of: http://www.vbaexpress.com/forum/showthread.php?2512-Solved-Position-on-page - Start at the end.
And https://www.techrepublic.com/article/use-vba-reach-beginning-end-microsoft-word-document/

Changing your original code to:
If ActiveDocument.Bookmarks.Exists(sBkMk) = True Then
ActiveDocument.Range.Characters.Last.Select
ActiveDocument.Bookmarks(vBkMk).Select

Should work ...

Hope this helps someone.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:51
Joined
Feb 28, 2001
Messages
27,313
Thanks, Marshall, for the pointer.

I was avoiding anything to do with the word "SELECT" in Word context because of issues I later explored when creating a new Word document that had index entries in it. However, where SELECT doesn't have any serious side effects and we are only talking about positioning, that looks good.
 
Local time
Today, 04:51
Joined
Feb 28, 2023
Messages
630
The main issues I've heard with "Select" had to do with the macro recorder and Excel/Word Macros.

It uses "Select" liberally, i.e. RangeA.Select, RangeA.Copy, RangeB.Select, RangeB.Paste, where the macro would work the same and be faster with the select statements omitted. It isn't that Select hurts anything, it just is unnecessary and slows down the processing.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:51
Joined
Feb 28, 2001
Messages
27,313
In the context where I had trouble, I was creating a new Word document from scratch and wanted extremely precise formatting. I did what I did by not using SELECT because text insertion followed by format control followed by more text insertion because a problem. But by using RANGEs (only) I was able to get exactly what I wanted with limited or no trouble. It was the mixing of RANGE and SELECT issues that confused the process. I got it working and am satisfied with the results. There is a thread elsewhere in the WORD forum on some of those trials and tribulations.
 

Users who are viewing this thread

Top Bottom