Finding the next hyperlink from the cursor & navigation back & forward thru hyperlinks

J

Jose Robins

I am hoping someone would be able to help me on the following two
problems. I googled the newsgroups but I couldn't find any
satisfactory solutions to these problems.

Two problems:
1. I have several hyperlinks(to bookmarks within document) in a word
document. How can I via vba (a) follow a hyperlink if the cursor is
on a hyperlink field and (b) search for the next closest hyperlink
field. I tried the goto method to search for a field but I somehow
couldn't figure that out. Maybe this is a really simple problem, which
I am somehow missing???

2. From the various newsgroups, I kind of understand that word does
not have the in-built capability to go back and forth between
hyperlinks similar to a web browser. It does allow going back to the
last three edit positions (Shift + F5) which is not exactly the same
thing. So I made a macro which when run, inserts a bookmark at the
current cursor and another macro which allows going to the earlier
made bookmark after visiting a hyperlink. Is this pretty much the
general way to do it or is there some standard approach to doing
navigating back & forth.

Any help would be appreciated.
Thank you so much,
Jose
 
H

Helmut Weber

Hi Jose,
if I got it right, you may be looking for something
like this, to follow the hyperlink and to get
you back to where you came from. In fact, only
when you run the macro for the first time, a bookmark
is added, else the bookmark will be redefined.
Sub Makro2()
Dim oDcm As Document
Set oDcm = ActiveDocument
With Selection
If .Hyperlinks.Count Then
.Bookmarks.Add Name:="HerIwas"
.Hyperlinks(1).Follow
Else
oDcm.Bookmarks("HerIwas").Select
End If
End With
End Sub
---
The next hyperlink (direction forward) is:
---
Dim l As Long
Dim oDcm As Document
Set oDcm = ActiveDocument
Dim oRng As Range
Set oRng = Selection.Range
oRng.Start = 0
oRng.End = Selection.End
l = oRng.Hyperlinks.Count
On Error Resume Next
' if there is no more hyperlink
' you may add a message as you like
oDcm.Hyperlinks(l + 1).Range.Select
---
or do you mean the closest hyperlink
in any direction?
---
Improvements welcome
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
W

Word Heretic

G'day (e-mail address removed) (Jose Robins),

1. I would try the followhyperlink and then test selection as first
guess.

2. That is the accepted approach, yes. To finesse, start the bookmark
name with an _ to make it 'invis'.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Jose Robins reckoned:
 
J

Jose Robins

Thanks Helmut, for your reply. Somehow I got dragged into other things and I
got delayed in examining your reply. I'll check out your comments and will
post a reply in a couple of days.

Thanks once again,
Regards,
Jose
 
J

Jose Robins

Helmut,
Actually, I already do something similar to what you mentioned here to go
back & forth.

However my other problem still remains.
Specifically - I have several hyperlink fields in the document (created via
Insert>Cross Reference> Bookmark as hyperlink). How can I find the next
cross reference field from the current cursor position and visit that
bookmark.Right now I have figured out (possibly not the most elegant
solution) on how to visit a bookmark if the **entire field*** is selected.
Here is my code for that. I would like to modify it such that the next field
relative to the cursor will be selected thru vba as well, rather than the
field having to be manually selected with the mouse.

Also is there an easy way to automatically indent vba code while in the vba
editor of MS office? i.e similar to how emacs automatically indents code
depending on the "nested" levels of the statement.

Thanks,
Jose

Sub VisitCurrentHyperlink()
Dim MyField As Variant
Dim strText() As String
Dim BkmkName As String

If Selection.Fields.Count > 0 Then
'Extract the bookmark name from the field and go to that bookmark
Set MyField = Selection.Fields(1)
If MyField.Type = wdFieldRef Then
strText() = Split(MyField.Code.Text, " ") ' extract the
bookmark name
BkmkName = strText(2)
Selection.GoTo What:=wdGoToBookmark, Name:=BkmkName
End If
End If
End Sub
 
J

Jose Robins

Hmm... I guess, I figured this one out with google in case anyone else is
interested. Anyone??

I guess I got the main stuff from the following post ....
http://groups.google.com/groups?hl=...ic.word.vba*&hl=en&lr=&ie=UTF-8&start=20&sa=N
by Peter Hewett...

Anyway here is the full code...

'-------------------------------------------------------------
Sub VisitCurrentHyperlink()
Dim MyField As Variant
Dim strText() As String
Dim MyObject As Variant
Dim BkmkName As String
If SelectionContainsAField() = True Then
'Extract the bookmark name from the field and go to that bookmark
Set MyField = FirstFieldInSelection()
If MyField.Type = wdFieldRef Then
strText() = Split(MyField.Code.Text, " ") ' extract the
bookmark name
BkmkName = strText(2)
Selection.GoTo What:=wdGoToBookmark, Name:=BkmkName
End If
End If
End Sub
'-------------------------------------------------------------
Public Function FirstFieldInSelection() As Word.Field
Dim rngTemp As Word.Range
Dim fldItem As Word.Field

' If the insertion point is in a field the field count is zero.
' This code allows for that problem.

' Return the first field in the selected range
If Selection.Fields.Count > 0 Then
Set FirstFieldInSelection = Selection.Fields(1)
Exit Function
End If

' Extend range to include current paragraph, if Selection is in a field
' the range is expanded to include that field
Set rngTemp = Selection.Range
rngTemp.Expand wdParagraph

' Make sure IP is really in a field and it's not just
' the expanded paragragh that picked up a field
For Each fldItem In rngTemp.Fields
If Selection.InRange(fldItem.Result) Then
Set FirstFieldInSelection = fldItem
Exit For
End If
Next
End Function ' FirstFieldInSelection

'-------------------------------------------------------------
'This code returns a field if the selection contains a field, however it
'returns Nothing if there is no field to return. If you need to test whether
'the selection (or insertion point) is within or contains a field use this:

Public Function SelectionContainsAField() As Boolean
Dim rngTemp As Word.Range
Dim fldItem As Word.Field

' If the insertion point is in a field the field count is zero.
' This code allows for that problem.

' No more to do if the selected range contains a field
If Selection.Fields.Count > 0 Then
SelectionContainsAField = True
Exit Function
End If

' Extend range to include current paragraph, if Selection is in a field
' the range is expanded to include that field
Set rngTemp = Selection.Range
rngTemp.Expand wdParagraph

' Make sure IP is really in a field and it's not just the
' expanded paragragh that picked up a field
For Each fldItem In rngTemp.Fields
If Selection.InRange(fldItem.Result) Then
SelectionContainsAField = True
Exit For
End If
Next
End Function ' SelectionContainsAField
 
H

Helmut Weber

Hi Jose,
.... this is getting pretty complicated.
In case, that your selection is between two fields,
I figured out something that gets you to the next field
in any direction. The remainder, insert bookmark, if
there is a hyperlink, hyperlink follow, get back,
should not be a problem any more.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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