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.
Microsoft 365VBAMicrosoft Word

Avatar of undefined
Last Comment
Jason Steward

8/22/2022 - Mon
Paul Sauvé

in any Word document, shift-F5 will bring you back to the last 3 positions of the cursor
Flyster

Try this:
Private Sub CommandButton1_Click()

    Application.GoBack
    
End Sub

Open in new window

Paul
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Flyster

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.
Jason Steward

ASKER
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...
Jason Steward

ASKER
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...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Qlemo

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.
Jason Steward

ASKER
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.
Qlemo

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Jason Steward

ASKER
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?
Qlemo

Sorry - no.
Paul Sauvé

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Neil Fleming

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jason Steward

ASKER
Thanks!  I'll try both of these!