1

I'm trying to write a VBA macro in Microsoft Word to do the same thing as Ctrl+click does (follow a link or go to the bookmark).

I've tried SendKeys but I don't think that works for left mouse click.

I've actually came up with a partially working solution involving the use of Selection.GoTo What:=wdGoToBookmark, Name:=BLAbut this unfortunately means I can't use ctrl+< because it seems that the history of where the cursor previously was is not saved.

So instead of coming up with my own solution, is there actually a way to just bind the action of Ctrl+click to another button? Or is there a way to write a macro that'll do the same action including keeping track of the history of the cursor?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Jimmy T
  • 447
  • 6
  • 14
  • Might this, in any way, be useful?:: https://stackoverflow.com/questions/3166265/open-an-html-page-in-default-browser-with-vba – Busy Sep 13 '17 at 05:31
  • Try `Selection.Hyperlinks(1).Follow`. Use the marco recorder to get hints for coding. – BitAccesser Sep 13 '17 at 06:11
  • Both of the above comments are strictly only for hyperlinks. I'm more interested in bookmarks (but since Ctrl+click by default works for both, I mentioned both). – Jimmy T Sep 13 '17 at 06:33
  • What, exactly, do you wish to accomplish? Select a hyperlink, run the macro, and then what? Logically, the selection should change to the destination of the hyperlink. So, you want another action (how to trigger?) to return the selection to where you came from? Is that your intention? – Variatus Sep 13 '17 at 06:34
  • I want to follow a bookmark link and be able to use Ctrl+< to navigate back to where I was before. Really I just want to bind Ctrl+click to a different button on the keyboard, but since that seems impossible, I want to try and replicate the Ctrl+click behaviour. Following a bookmark link as I mentioned in my post was not enough because I couldnt Ctrl+< back to the previous spot, so I'm obviously missing something. – Jimmy T Sep 13 '17 at 07:26
  • Seems like you can create [shortcuts](https://support.office.com/en-us/article/Customize-keyboard-shortcuts-9a92343e-a781-4d5a-92f1-0f32e3ba5b4d). If you create one for follow bookmark and one for back you should be able to store position on follow and restore it on back. – BitAccesser Sep 13 '17 at 08:03

1 Answers1

0

The following code should do what you want. Install it on a standard code module.

Option Explicit

    Dim ReturnRange As Range

Sub GotoBookmark()
    ' 13 Sep 2017

    With Selection
        If .Hyperlinks.Count Then
            Set ReturnRange = .Range
            .Hyperlinks(1).Follow
        End If
    End With
End Sub

Sub ReturnToLink()
    ' 13 Sep 2017

    If Not ReturnRange Is Nothing Then ReturnRange.Select
End Sub

For testing purposes, create a bookmark in your document and a hyperlink to it. Select the hyperlink and run Sub GotoBookmark. Then run procedure ReturnToLink to go back to where you came from. Note that you can return from anywhere as well as multiple times.

You may wish to create keyboard shortcuts to call the two subs.

Variatus
  • 14,293
  • 2
  • 14
  • 30