Retore original screen when macro ends

G

Geoff Budd

After running a macro, I would like to try and restore the screen to the
exact view that the user had before running the macro - i.e. on the correct
page and at the correct line, with the cursor in its original position and
the screen scrolled to its original point (i.e. not necessarily at the top of
the page if that was not the exact screen position). That is, so that the
user sees no change to his/her view after the macro has ended.

I presume that somehow I have to save all the parameters that define such
things as page number, cursor position, zoom ratio, scroll position, etc; and
then restore them afterwards. Is there any way of doing this?

Many thanks,
Geoff
 
P

Pesach Shelnitz

Hi Geoff,

The use of the Selection object is the most common cause of changes in the
curosr position on the screen when a macro runs. If this is the cause, you
can use the Range object instead of the Selection object to avoid cursor
movement and other display changes. However, there are other possible causes.
If you would like a more complete answer for your specific case, you will
need to show your code to this group.
 
G

Geoff Budd

Hi Pesach,
I don't think I've explained myself too well ...
I don't necessarily want to avoid changes in cursor position on the screen
during the running of a macro - I just want to find a way of effectively
saving the current screen and cursor position at the start of a macro, and
then restoring it to exactly the same state when the macro has finished.
I don't have any particular code in mind as this is a general question that
can apply to a number of operations that are carried out by a macro - the
overall object is to restore the screen to exactly the same state that it was
in before the macro started, so that the user can carry on wherever they left
off without having to scroll back to wherever they were before the macro
started.
I hope this is a little clearer now.
Many thanks,
Geoff
 
P

Pesach Shelnitz

Hi Geoff,

One general approach is to create a bookmark at the current cursor position
and then return to the bookmark when the macro finishes. Of course, for this
to work, the macro must not delete the bookmark. The following code is based
on this approach.

ActiveDocument.Bookmarks.Add name:="MacroStartPosition", _
Range:=Selection.Range
' Insert the rest of the macro code here.
If ActiveDocument.Bookmarks.Exists("MacroStartPosition") = True Then
ActiveDocument.Bookmarks("MacroStartPosition").Select
Else
MsgBox "The original cursor position could not be restored."
End If

If your macro moves to another document, your code will need to return to
the starting document before going back to the bookmark.
If your macro changes the type of view, it will need to return to the
starting type of view.
This is a general approach, and there may be cases in which a different
approach will be needed.
 
G

Geoff Budd

Thanks Pesach - that's a very neat soloution.

I just need to make sure that the vertical scroll bar is in the same
position at the end as it was at the beginning now, so the user sees exactly
the same page view on the screen.

Regards,
Geoff
 
J

Jay Freedman

Geoff, just don't set your expectations too high. VBA's control of the
screen position isn't very exact, and it gets worse as the document
becomes longer or is modified.

The VBA control of the vertical scrollbar position is in terms of a
percentage of the total length of the document, through the
ActiveWindow.VerticalPercentScrolled property. For reasons known only
to Microsoft, although that property is documented as being of type
Long, it behaves like an Integer. If the document is long enough, that
may not let you return to a view with exactly the same line at the top
of the screen.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Geoff Budd

Thanks for this, Jay - I guess that's the best we can do.

Thanks to you and Pesach for your time and helpful suggestions,
Geoff
 

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