Macro for Hyperlinking

R

Rebecca

Greetings. I am using MS Windows / Office XP. I am
unable to figure out how to do this on my own, so I am
asking the experts here in this newsgroup for help.

I have a bunch of files containing containing Bible verses
along with Greek, Hebrew, and English material, one
document of which (e.g., the Gen.doc) looks something like
the following:

Gen 1:1
blah, blah, blah

Gen 1:2
blah, blah, blah

etc., etc.

I need a macro that can do the following:

Go to each Bible verse, such as the Gen 1:1 verse above,
copy it, and in another file (called "Hyperlinks.doc")
insert / paste the hyperlink, then go back to
the "Gen.doc," go down to the next verse (Gen 1:2), and so
on, so that eventually the "Hyperlink.doc" will look like
the following (though the underlining indicating
hyperlinks cannot be shown here, of course:

Gen 1:1 Gen 1:2 Gen 1:3, etc.,

all of which are linked to each of the verses in
the "Gen.doc." So when I click on Gen 1:1 in
the "Hyperlink.doc" I will be taken to the appropriate
verse in the "Gen.doc." Could someone please explain in
simple English how I can go about making such a macro?
Thanks.
 
D

DA

Hi Rebecca

You were after a solution in 'simple English', and I
suppose a simple explanation would be to do your tasks
manually and use the macro recorder to get some idea on
what's going on behind the scenes.

I gather though that this may not be enough to help you
out. I've included one suggestion (and there are many
alternatives here) on how you could write a macro to do
your hyperlinks.

You didn't give any indication as to what your skills are
in understanding VBA code, but I hope the following will
give you at least some ideas on how to proceed further.

Basically, what I'm doing in my sample is as follows:

- I have an open document, called "Hyperlink.doc",
containing my macro.
- I used a set of other test documents that contained
text such as:

GEN 1:1
Sample text..
GEN 1:2
More text.. and so on

- The macro prompts for a directory and then searches for
all doc files in that directory.

- The code will open each document it finds and search
for the string "GEN". It then creates bookmarks for each
occurrence and saves this info in an array.

- Each document is saved with the new bookmarks and the
array data is used to generate your links in
the "Hyperlinks" document.

Hope this is of some help to you. If you can't get this
to work or get totally stuck, just do another post.

All the best,
Dennis

(Sample)
---------------
Sub GENHypLinks()
Dim strInputDir As String
Dim astrBmarks() As String
Dim lngCounter As Long
Dim lngCounter2 As Long

strInputDir = InputBox("Enter Input Directory")

With Application.FileSearch
.NewSearch
.LookIn = strInputDir
.FileName = "*.doc"
.SearchSubFolders = False
.Execute

For lngCounter = 1 To .FoundFiles.Count
Documents.Open FileName:=.FoundFiles(lngCounter)

Call SearchGEN(astrBmarks)
ActiveDocument.Save
ActiveDocument.Close

Selection.MoveEnd Unit:=wdStory
Selection.MoveRight
lngCounter2 = 0

Do While astrBmarks(lngCounter2, 0) <> ""
Selection.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=.FoundFiles(lngCounter), _
SubAddress:=astrBmarks(lngCounter2, 1), _
TextToDisplay:=astrBmarks(lngCounter2, 0)
lngCounter2 = lngCounter2 + 1
Loop

Next lngCounter
End With
End Sub

'This routine searches for the "GEN" references in
'your document.
Private Sub SearchGEN(astrBmarks)
Dim lngCounter As Long

' This line sizes the array to allow for 600 records
' per document. You could write a routine that counts the
' number of GEN references first in order to make this
' more flexible.
ReDim astrBmarks(600, 1)

With Selection
.HomeKey Unit:=wdStory
Do While .Find.Execute(FindText:="GEN", _
Forward:=True, MatchCase:=True)
.Bookmarks.Add (Selection + CStr(lngCounter))

'Boomarks don't allow for punctuation or spaces.
'The Expand is used to capture a more unique
'reference that you can use as your hyperlink text.
.Expand Unit:=wdParagraph

'Save the bookmark and link text to the array
astrBmarks(lngCounter, 0) = Selection
astrBmarks(lngCounter, 1) = .Bookmarks(1).Name

.MoveRight
lngCounter = lngCounter + 1
Loop
End With
End Sub
 
R

Rebecca

Thanks for your help, Dennis. You are a peach.
-----Original Message-----
Hi Rebecca

You were after a solution in 'simple English', and I
suppose a simple explanation would be to do your tasks
manually and use the macro recorder to get some idea on
what's going on behind the scenes.

I gather though that this may not be enough to help you
out. I've included one suggestion (and there are many
alternatives here) on how you could write a macro to do
your hyperlinks.

You didn't give any indication as to what your skills are
in understanding VBA code, but I hope the following will
give you at least some ideas on how to proceed further.

Basically, what I'm doing in my sample is as follows:

- I have an open document, called "Hyperlink.doc",
containing my macro.
- I used a set of other test documents that contained
text such as:

GEN 1:1
Sample text..
GEN 1:2
More text.. and so on

- The macro prompts for a directory and then searches for
all doc files in that directory.

- The code will open each document it finds and search
for the string "GEN". It then creates bookmarks for each
occurrence and saves this info in an array.

- Each document is saved with the new bookmarks and the
array data is used to generate your links in
the "Hyperlinks" document.

Hope this is of some help to you. If you can't get this
to work or get totally stuck, just do another post.

All the best,
Dennis

(Sample)
---------------
Sub GENHypLinks()
Dim strInputDir As String
Dim astrBmarks() As String
Dim lngCounter As Long
Dim lngCounter2 As Long

strInputDir = InputBox("Enter Input Directory")

With Application.FileSearch
.NewSearch
.LookIn = strInputDir
.FileName = "*.doc"
.SearchSubFolders = False
.Execute

For lngCounter = 1 To .FoundFiles.Count
Documents.Open FileName:=.FoundFiles(lngCounter)

Call SearchGEN(astrBmarks)
ActiveDocument.Save
ActiveDocument.Close

Selection.MoveEnd Unit:=wdStory
Selection.MoveRight
lngCounter2 = 0

Do While astrBmarks(lngCounter2, 0) <> ""
Selection.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=.FoundFiles(lngCounter), _
SubAddress:=astrBmarks(lngCounter2, 1), _
TextToDisplay:=astrBmarks(lngCounter2, 0)
lngCounter2 = lngCounter2 + 1
Loop

Next lngCounter
End With
End Sub

'This routine searches for the "GEN" references in
'your document.
Private Sub SearchGEN(astrBmarks)
Dim lngCounter As Long

' This line sizes the array to allow for 600 records
' per document. You could write a routine that counts the
' number of GEN references first in order to make this
' more flexible.
ReDim astrBmarks(600, 1)

With Selection
.HomeKey Unit:=wdStory
Do While .Find.Execute(FindText:="GEN", _
Forward:=True, MatchCase:=True)
.Bookmarks.Add (Selection + CStr(lngCounter))

'Boomarks don't allow for punctuation or spaces.
'The Expand is used to capture a more unique
'reference that you can use as your hyperlink text.
.Expand Unit:=wdParagraph

'Save the bookmark and link text to the array
astrBmarks(lngCounter, 0) = Selection
astrBmarks(lngCounter, 1) = .Bookmarks(1).Name

.MoveRight
lngCounter = lngCounter + 1
Loop
End With
End Sub
---------------



.
 

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