go to bookmark from listbox

M

Martin

Hi,

I have a long document with numerous bookmarks. I
populate a listbox, from the control toolbox, with the
names of the bookmarks. I want to go the selected
bookmark - and can't quite figure out how to do it. Any
help is greatly appreciated.

TIA

Martin

Here is code

Private Sub Document_Open()
Dim i As Integer
i = Documents("III Audit Cases").Bookmarks.Count
On Error Resume Next
For i = 1 To Documents("III Audit Cases").Bookmarks.Count
On Error GoTo 0
With ListBox1
..AddItem Documents("III Audit Cases").Bookmarks(i).Name
End With
Next i
End Sub

Private Sub ListBox1_Click()
Dim i As Integer
Dim myselection As String
myselection = ListBox1.List(i)
Selection.GoTo What:=wdGoToBookmark, Name:=myselection
End Sub
 
P

Peter Hewett

Hi Martin

Try the following code:

Private Sub ListBox1_Click()

With ListBox1
If .ListIndex >= 0 Then
ActiveDocument.Bookmarks(.Value).Select
End If
End With
End Sub

HTH + Cheers - Peter
 
M

Martin

Thank you and to Peter,

I still have the same problem as before in that when a
selection is made the curser goes to the 8th item in the
list. If I put a break in the code the proper attributes
display and it goes to the right place. If I take the
break out it goes to the 8th item???

The listbox properties Value Property displays the text
for the eighth item.

Any additional insight is appreciated,

Martin
 
P

Peter Hewett

Hi Martin

When I first looked at the code I didn't see that you were using a ListBox
control dropped onto a document. Jus a comment unless your loading the ListBox
from another document the "Documents("III Audit Cases")." in your code is
redundant.

Have you protected your document are the bookmarks in a protected section of
your document?

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Martin

I've just done some further testing it works fine using the code I posted.

I've tidied the code up try this:

Private Sub Document_Open()
Dim bmItem As Word.Bookmark

ActiveDocument.Bookmarks.ShowHidden = False
For Each bmItem In ActiveDocument.Bookmarks
With ListBox1
.AddItem bmItem.Name
End With
Next
End Sub

Private Sub ListBox1_Click()

With ListBox1
If .ListIndex >= 0 Then
ActiveDocument.Bookmarks(.Value).Select
End If
End With
End Sub

HTH + Cheers - Peter
 
M

Martin

Hi Peter,

Thank you for your followup.

The problem on my end persists My Word is from office xp.
There are 11 cases in the document (no protection) There
is a page break between each case. There is a heading
followed by one or two short paragraphs. The bookmark is
inserted in front of the heading. A typical heading would
be "Limousine Service" and the bookmark name in this
instance would be "Limousine_Service". The insertion
point of the bookmark is before the L in this instance.

The subject matter is in random order, bookmarks are
sorted alphebetically and there are no hidden bookmarks.

In each click event the selection moves to the eighth item
which is the last item in the listbox.

I modified your code to try and trap an error - an error
occurs but I can't identify it errormumber is "".

This has me stumped'

Martin
 
P

Peter Hewett

Hi Martin

I used Word XP to produce the code and it worked for me.

To determine what the problem is/are you need to isolate what's going on. So
create a new document, just one page add four lines, select each line in turn
and create a bookmark for it. Now drop an ActiveX ListBox control on the form
and add the code I posted to the ThisDocument module. Does it work???

Cheers - Peter

Hi Peter,

Thank you for your followup.

The problem on my end persists My Word is from office xp.
There are 11 cases in the document (no protection) There
is a page break between each case. There is a heading
followed by one or two short paragraphs. The bookmark is
inserted in front of the heading. A typical heading would
be "Limousine Service" and the bookmark name in this
instance would be "Limousine_Service". The insertion
point of the bookmark is before the L in this instance.

The subject matter is in random order, bookmarks are
sorted alphebetically and there are no hidden bookmarks.

In each click event the selection moves to the eighth item
which is the last item in the listbox.

I modified your code to try and trap an error - an error
occurs but I can't identify it errormumber is "".

This has me stumped'

Martin

HTH + Cheers - Peter
 
M

Martin

Thank you Peter,

I replicate your results under test conditions using four
bookmarks. When I forced page breaks between the
bookmarks I could not select the fourth one from the
listbox It defaulted to the third one. I shall try and
get the communications going first - using an extra
bookmark & pasting the script in later. Will post back if
problems - Thank you for all your help on this, I learned
something.

Martin
 

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