PDA

View Full Version : macro to remember page number where last hyprlink was accessed



zahoor
12-10-2016, 11:30 PM
I am looking for a macro which can remember page number where last HYPERLINK was accessed in a word ducument.

gmayor
12-11-2016, 03:28 AM
It's a little fiddly to do, but it can be achieved with a macrobutton field wrapped around the hyperlink which it then uses as the prompt text and a couple of macros - one for the macrobutton field - here GetLink, which bookmarks the field as "bmLastLink". Obviously if you are are already using that bookmark in your document, you need to change the name the one in both macros to one you are not using.

When you double click the macrobutton field, the macro runs to add the bookmark and follow the link where it leads.

The second macro returns you to the bookmarked field.

{ MacroButton GetLink { HYPERLINK \l "_Ut_wisi_enim" } }




Option Explicit

Sub GetLink()
'Graham Mayor - http://www.gmayor.com - Last updated - 11/12/2016
Dim oRng As Range
Dim oFld As Field
Dim oLink As Hyperlink
If Selection.Fields.Count = 0 Then
MsgBox "Selection is not in a field!"
GoTo lbl_Exit
End If
Set oFld = Selection.Fields(1)
Set oRng = oFld.code
Set oLink = oRng.Hyperlinks(1)
ActiveDocument.Bookmarks.Add "bmLastLink"
ActiveDocument.FollowHyperlink oLink.Address, oLink.SubAddress
lbl_Exit:
Exit Sub
End Sub

Sub ReturnToLink()
'Graham Mayor - http://www.gmayor.com - Last updated - 11/12/2016
Dim oBM As Bookmark
For Each oBM In ActiveDocument.Bookmarks
If oBM.Name = "bmLastLink" Then
oBM.Select
Exit For
End If
Next oBM
lbl_Exit:
Exit Sub
End Sub

Yuri
12-15-2016, 05:48 AM
. . .
If Selection.Fields.Count = 0 Then
MsgBox "Selection is not in a field!"
GoTo lbl_Exit
End If
. . .

gmayor, it seems like the "Selection.Fields.Count" does not always return number of fields in the selection if the cursor is in a field (I checked it in the mode of viewing field codes). Microsoft introduced the "Selection.Information(wdInFieldCode)" in Word 2013. Previously, one had to write a VBA Sub to deteremine if the cursor is in a field.

gmayor
12-15-2016, 07:51 AM
The field count was primarily placed should the user run the macro other than from a macrobutton field. Users have a tendency to click things that shouldn't really be clicked.

When the selection is a macrobutton field the field count will show 2 if there is a hyperlink in the macrobutton field as used here for a prompt text. Field Index 1 is the macrobutton field, index 2 is the hyperlink field. I suppose I could have error checked for a second field that was not a hyperlink field, but in the context of what the macro was doing, I thought it unnecessary overkill.

The macrobutton is not used with the field showing field codes. You cannot operate the field by clicking in that mode.

Yuri
12-15-2016, 08:23 AM
. . .
The macrobutton is not used with the field showing field codes. You cannot operate the field by clicking in that mode.
. . .

Sorry, it's my misunderstanding. And what if Zahoor has other types of hyperlink fields (both HYPERLINK and REF with /h switch) - how to intercept those?

gmayor
12-16-2016, 12:11 AM
The method I suggested was intended to allow a user to return to the hyperlink by means of a macro which bookmarks the original location enabling the second macro to return focus to the original location. The macrobutton operates the hyperlink in the macrobutton field.