Link to home
Start Free TrialLog in
Avatar of Jason Steward
Jason Steward

asked on

Microsoft Word- Return to Previous Cursor/Edit Position Using A Button

I have a Definitions section in my Word document.  I am using Office 365.  The document is an owner's manual, and I am referencing the same definitions multiple times.  The definitions are 200 pages past the primary references.  I'd like to be able to click on a hyperlink taking me to the definition of a word (already done that...  easy), THEN once I'm there, I'd like to click on a button or a hyperlink to take me back to the original position/cursor location/page regarless of where in the document user was reading on prior to navigating to the definition of a key term.

I tried using a button with SendKeys in VBA:  
Private Sub CommandButton1_Click()

    VBA.SendKeys ("{Ctrl}{Alt}{Z}")
    
End Sub

Open in new window

I get a Runtime Error #5 with this, "Invalid procedure call or argument".

My first preference would be to hyperlink to return to previous page, but I can't seem to find any way to accomplish this.
Avatar of Paul Sauvé
Paul Sauvé
Flag of Canada image

in any Word document, shift-F5 will bring you back to the last 3 positions of the cursor
Try this:
Private Sub CommandButton1_Click()

    Application.GoBack
    
End Sub

Open in new window

Paul
Avatar of Jason Steward
Jason Steward

ASKER

Thanks Flyster!  That's the kind of thing I was looking for.  Much easier to have a user click a button that says "Back" or "Return" than it is to teach random people you don't know how to hold down Shift and F5.  I used your code and didn't get an error message, but it didn't do anything.  When reader clicks a hyperlink from top of document bringing them to the definitions page, pressing the "Back" button does nothing.  It doesn't bring them back to where they were.  So I tried the Shift + F5 as SendKeys and got the same Invalid Procedure error message as mentioned in my original post.
The VBA code for Shift F5 is:
VBA.SendKeys ("+{F5}")

Open in new window

Are the hyperlinks to the definitions located on the same page? If so, you can put a bookmark on that page and use this code for your back button:
Selection.GoTo What:=wdGoToBookmark, Name:="Top"

Open in new window

Just change the word "Top" to whatever you name your bookmark.
Holding Shift and pressing F5 works, but the code
 
Private Sub CommandButton1_Click()
    VBA.SendKeys ("+{F5}")
End Sub

Open in new window

does nothing when the button is pressed.

I think what I'm going to do is make a text box and create hyperlinks to various pages towards the top of the document.  That way after users click to see the definition they can click to get at least close to where they were reading before.  It just seems crazy to me there isn't a common solution to return readers without a background in computer science to a previous location in a document...
One more note for anyone following behind this who wants to keep trying something to make this work...

The Shift + F5 suggestion above would return a user to previous edit points according to what I've read online.  If you are creating a user guide like me, users would not be editing the guide so that wouldn't work.  Similarly, I also found a method online using {Ctrl}{Alt}{Z}, but that returns users to previous cursor position and wouldn't work for a user guide.  What is really needed is something to return users to the previous view...  because users of the guide aren't clicking and editing, they're reading when they click on the hyperlink and I'm not so sure the cursor is changed when clicking on the hyperlinks.  

ALT+LEFT ARROW returns to previous view and seems to be perfect for navigating through hyperlinks.  However, I tried this code:
Private Sub CommandButton1_Click()
        VBA.SendKeys "%{LEFT}", True 
End Sub

Open in new window

and while it works manually typed from a keyboard, the SendKeys method does nothing...  no error, just does nothing when button is pressed.  I'm possibly not executing this right.  Maybe someone someday can figure out how to make it work and change MS Word forever...
When you say "pressing the "Back" button does nothing", do you mean the button as described in https://cybertext.wordpress.com/2010/04/21/word-add-a-back-button-to-the-toolbar/ ? Because that one should do the same as Alt-Csr Left.
No.  I meant the button I created as per my description which I labeled "Back".  The button should take me to the previous page I was viewing before going to the hyperlink to see a definition in another part of the 1,000+ page document.

Just imagine yourself as a standard user with enough computer knowledge to turn on a computer and open a file for viewing.  The primary reason the user is consulting with this guide is because they are not an advanced computer person.  I will never have a chance to speak with or instruct these people on how to hold down keys, press CTRL, and other secret methods of navigating around a document.  I simply need them to click a hyperlink to see a definition (that's located at bottom of document), then they can click on a button (or a link) that returns them to their previous page.
It does not look like you can run a macro to simulate Alt-L (WebGoBack). So all I can think of is, as mentioned above, to replace the jump action to the dscription by code creating a pre-defined fixed bookmark (say YourBookmark), and then having a button executing Selection.GoTo what := wdGoToBookmark, name := YourBookmark to jump back again.
I read some things on the bookmark, but I couldn't come up with a complete understanding of the code so that I could customize that solution for my particular document and need.  I'm just not that advanced.  Any ideas on another approach that show a user a Definition of a word when user clicks on the word?
Sorry - no.
i imagine that you have seen this:
Create or run a macro
Record a macro with a button

i put the button on the QAT (Quick Access Toolbar) and it works. However, i don't know if it will show up ob the QAT of another computer just by opening the document

i included the document as an example
darktable-workflow.docx
ASKER CERTIFIED SOLUTION
Avatar of Neil Fleming
Neil Fleming
Flag of United Kingdom of Great Britain and Northern Ireland 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
Thanks!  I'll try both of these!