how can I creates a bookmark and adds a hyperlink and go back to .

D

dapanpan

Hi erveryone,
I have VBA code in Word 2003 that creates a bookmark and adds a hyperlink of
it at the beginning of the document. As the bookmark name is known, how can I
go back to the bookmark?

Any thoughts would be greatly appreciated. Thanks.
 
D

Doug Robbins - Word MVP

ActiveDocument.Bookmarks("nameknownbookmark").Range.Select

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

James

Hi dapanpan

I was looking for something that does what your VBA code does. It would save
me a LOT of time in a future project I have. Is it possible for you to post
it here? Thanks.
 
G

Graham Mayor

You would be better posting in a vba programming forum rather than tacking a
message to an ancient post, to which end I have cross-posted this reply to
the vba general forum. If you can explain exactly what you wish to achieve,
I am sure that someone will be able to assist. You will need also to confirm
which Word version you are using.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

James

Ok, I will. Thanks.

Graham Mayor said:
You would be better posting in a vba programming forum rather than tacking a
message to an ancient post, to which end I have cross-posted this reply to
the vba general forum. If you can explain exactly what you wish to achieve,
I am sure that someone will be able to assist. You will need also to confirm
which Word version you are using.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

James

Hi again

I will soon start working again on a personal project that requires adding
more than 200 bookmarks in a huge Word document. After inserting the
bookmarks I should create hyperlinks to all those bookmarks at the beginning
of the document.
A friend helped me in a previous project by creating a macro that
automatically creates all those hyperlinks. So all I had to do was inserting
the bookmarks manually (by going to the first location, pressing
CTRL+SHIFT+F5, naming the bookmark "p1", then move to second location,
pressing CTRL+SHIFT+F5, naming the bookmark "p2", etc). After inserting
hundreds of bookmarks this way, I used my friend's macro to create the
hyperlinks to them.

What I'm asking is whether there's an easier way to do it, something like
putting the cursor in the first location in the document, running a macro
that automatically creates a bookmark (naming it "1" or "p1" or anything with
a number that adds up consecutively) then creates a hyperlink at the
beginning of the document. I have Word 2003.

The document is an electronic version of a book. The bookmarks are for the
start of each page of the book. Please note that the electronic book is
available for free on the Internet and no copyright is infringed.

Thank you
 
G

Graham Mayor

On reflection, I suspect a table of contents would be better suited to this,
which would give you your hyperlinks to the TC fields in the document. The
only problem is in the naming of the Hyperlinks - though the numbering is
easily handled with a docvariable

If you run the first macro - InsertTCField
it will set the initial variable value to 1 (you can reset that number with
the second macro ResetVarTC) then insert a TOC entry field with the text
"Location n" where n is the incrementing number from the docvariable field
and "Location " the fixed text.

Each time you run the macro you will get an incremented TOC entry field.

The final macro will insert the hyperlinks (the table of contents) at the
start of the document

Sub InsertTCField()
Dim oVars As Variables
Dim sNum As Long
Set oVars = ActiveDocument.Variables
Start:
On Error GoTo NoVar
sNum = oVars("varTC").Value
Selection.Fields.Add Selection.Range, wdFieldTOCEntry, _
Chr(34) & "Location " & sNum & Chr(34), False
oVars("varTC").Value = oVars("varTC").Value + 1
Exit Sub
NoVar:
oVars("varTC").Value = 1
GoTo Start
End Sub

Sub ResetVarTC()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varTC").Value = InputBox("Enter new start number", _
"TOC Variable Number", oVars("varTC").Value)
End Sub

Sub InsertTOCField()
With Selection
.HomeKey wdStory
With .Fields
.Add Selection.Range, wdFieldTOC, "\f \n \h \z", False
.Update
End With
.TypeParagraph
End With
With ActiveWindow.View
.ShowFieldCodes = False
.ShowHiddenText = False
End With
End Sub

http://www.gmayor.com/installing_macro.htm
 
G

grammatim

Why would you distribute the book as a Word file rather than as a pdf?

If you're opening it in Word, why not simply use the GoTo command to
go to a specific page?
 
J

James

Hi grammatim

It's because I want to convert the Word document to HTML then convert it to
iSilo format for use on my PDA.
 
G

Graham Mayor

On further reflection, in an effort to make the links more meaningful, the
following macro adds selected text in place of 'Location'. It requires that
you select the text to link to before running the macro. Then puts that text
followed by the number.

Sub InsertTCField()
Dim oVars As Variables
Dim sNum As Long
Dim oRng As Range
Set oVars = ActiveDocument.Variables
Set oRng = Selection.Range
If Len(oRng) = 0 Then
MsgBox "No text selected" & vbCr & _
"Select the text and re-run the macro", _
vbCritical, "Error"
Exit Sub
End If
Start:
On Error GoTo NoVar
sNum = oVars("varTC").Value
Selection.Fields.Add Selection.Range, wdFieldTOCEntry, _
Chr(34) & oRng.Text & sNum & Chr(34), False
oVars("varTC").Value = oVars("varTC").Value + 1
Exit Sub
NoVar:
oVars("varTC").Value = 1
GoTo Start
End Sub

or to use Location where no text is selected and avoid the prompt

Sub InsertTCField()
Dim oVars As Variables
Dim sNum As Long
Dim oRng As Range
Set oVars = ActiveDocument.Variables
Set oRng = Selection.Range
If Len(oRng) = 0 Then
oRng.Text = "Location "
End If
Start:
On Error GoTo NoVar
sNum = oVars("varTC").Value
Selection.Fields.Add Selection.Range, wdFieldTOCEntry, _
Chr(34) & oRng.Text & sNum & Chr(34), False
oVars("varTC").Value = oVars("varTC").Value + 1
Exit Sub
NoVar:
oVars("varTC").Value = 1
GoTo Start
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Oops! Sorry I didn't check that before posting: Try

Sub InsertTCField()
Dim oVars As Variables
Dim sNum As Long
Dim sText As String
Dim oRng As Range
Set oVars = ActiveDocument.Variables
Set oRng = Selection.Range
sText = oRng.Text
If Len(oRng) = 0 Then
'MsgBox "No text selected" & vbCr & _
' "Select the text and re-run the macro", _
' vbCritical, "Error"
'Exit Sub
sText = "Location"
End If
Start:
On Error GoTo NoVar
sNum = oVars("varTC").Value
Selection.Collapse wdCollapseStart
Selection.Fields.Add Selection.Range, wdFieldTOCEntry, _
Chr(34) & sText & Chr(32) & sNum & Chr(34), False
oVars("varTC").Value = oVars("varTC").Value + 1
For i = 1 To ActiveDocument.Fields.Count
If ActiveDocument.Fields(i).Type = wdFieldTOC Then
ActiveDocument.Fields(i).Update
End If
Next i
Exit Sub
NoVar:
oVars("varTC").Value = 1
GoTo Start
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

James

Thanks for the correction. Of course, this part won't be needed.
'MsgBox "No text selected" & vbCr & _
' "Select the text and re-run the macro", _
' vbCritical, "Error"
'Exit Sub

Thanks again.
 

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