PDA

View Full Version : List of Bookmark in user-form for navigation as bookmark are in the tables(MS Word)



Nasir Ali
01-27-2016, 11:06 PM
I have 40 tables (MS Word 2010) and want to navigate to specific rows in those tables (MS Word 2010).

I bookmark the words in those rows and

now want to have a user-form that list the all the bookmark so clicking the bookmark lead to the bookmark.

regards,

gmayor
01-27-2016, 11:53 PM
Imagine a userform (http://www.gmayor.com/Userform.htm) with a combobox and a command button. The code for the userform could be


Option Explicit

Private Sub CommandButton1_Click()
If Me.ComboBox1.ListIndex < 1 Then
MsgBox "No Bookmark selected"
Me.ComboBox1.SetFocus
GoTo lbl_Exit
End If
Me.Hide
Me.Tag = 1
lbl_Exit:
Exit Sub
End SubThen in an ordinary module you would insert the code to call the userform and process the selection e.g.
Option Explicit

Sub Example()
Dim ofrm As New UserForm1
Dim oBM As Bookmark
With ofrm
With .ComboBox1
.AddItem "[Select Bookmark]"
For Each oBM In ActiveDocument.Bookmarks
.AddItem oBM.Name
Next oBM
.ListIndex = 0
End With
.Show
If .Tag = 1 Then
ActiveDocument.Bookmarks(.ComboBox1.Text).Range.Select
End If
End With
lbl_Exit:
Exit Sub
End Sub


If you wanted more meaningful descriptions, you could use a two column combo box with the bookmark names and a descriptive text and display only the latter, but let's not run before we can walk.

See also the FillBM function at http://www.gmayor.com/useful_vba_functions.htm

Nasir Ali
01-28-2016, 12:34 AM
thank for your immediate reply, unfortunately i got the blank combo box

control name on mine form are

User Form Name:UserForm1
ComboBox name:ComboBox1
Command Button Name:CommandButton1

when i run the macro "example" Userform appear with text"[Select Bookmark]"
while i click the command button msg box appear " no bookmark selected"

second i need to add one command button" cancel" to close the form.. what will be the code for click of that comamnd button.

thanks again

gmayor
01-28-2016, 04:36 AM
This is a combobox not a list box. It displays only the selected item, so it only needs to be the height of the text. You click the down arrow alongside to see the list. You will only have listed items if there are bookmarks in the document.
The userform will close when you click the button having made a selection.
If you want a cancel button the code for it would be

Private Sub CommandButton2_Click()
Me.Hide
Me.Tag = 0
lbl_Exit:
Exit Sub
End Sub

Nasir Ali
02-02-2016, 12:03 AM
Thanks a lot Mr. Graham Mayor for your help, i have realized my mistake or whatever we say it,( as i am very new in VBA) and i have correct it. but still it not server the purpose as i want to have list of all the bookmarks in the view.

I have find a far better solution googling , at link techrepublic


Option ExplicitPrivate m_gotoInProgress As Boolean
' Load bookmarks on bookmarks selection form activation.
Private Sub UserForm_Activate()
Dim bmk As Bookmark
lstBookmarks.Clear
For Each bmk In ActiveDocument.Bookmarks
lstBookmarks.AddItem (bmk.Name)
Next bmk
txtBookmarkName.SetFocus
End Sub
' Select bookmark using quick selection textbox.
Private Sub txtBookmarkName_Change()
If (Not isNullOrEmpty(txtBookmarkName.Text)) Then
Dim bmkName As Variant
Dim listIndex As Integer
listIndex = -1
For Each bmkName In lstBookmarks.List
listIndex = listIndex + 1
If (Len(bmkName) >= Len(txtBookmarkName.Text)) Then
If (UCase(Left(bmkName, Len(txtBookmarkName.Text))) = UCase(txtBookmarkName.Text)) Then
lstBookmarks.Selected(listIndex) = True
Exit For
End If
End If
Next bmkName
End If
End Sub
' Check if a string is null or empty.
Private Function isNullOrEmpty(ByVal s As String)
If (IsNull(s)) Then
isNullOrEmpty = True
ElseIf (Len(s) = 0) Then
isNullOrEmpty = True
End If
End Function
' Process [Enter] and [Esc] keys for quick selection textbox.
Private Sub txtBookmarkName_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Debug.Print KeyCode
If (KeyCode = 13) Then ' Enter
m_gotoInProgress = True
cmdGoto_Click
ElseIf (KeyCode = 27) Then ' Esc
cmdCancel_Click
End If
End Sub
' Go to selected bookmark.
Private Sub cmdGoto_Click()
On Error GoTo cmdGoTo_Click_Finally
If (IsNull(lstBookmarks.listIndex)) Then
MsgBox "There is no any bookmarks selected", vbExclamation + vbOKOnly, "Warning"
Return
End If
Selection.GoTo What:=wdGoToBookmark, Name:=lstBookmarks.List(lstBookmarks.listIndex)
cmdGoTo_Click_Finally
If (m_gotoInProgress) Then
m_gotoInProgress = False
DoEvents
txtBookmarkName.SetFocus
End If
End Sub
' Close bookmarks selection form.
Private Sub cmdCancel_Click()
Me.Hide End Sub

Only problem is when we click the go to command button , respective bookmark appear on the Word window but .. Use form remain focused ,we need to close/hide the user form to get to work on Word window.

15334

gmayor
02-02-2016, 02:30 AM
If you want to have all the items in view, then you need a list box rather than a combo box. The programming for a list box is essentially similar to that in my earlier reply, except for the reference to combo box, and you don't need the prompt line i.e. in an ordinary module
Option Explicit

Sub Example()
Dim ofrm As New UserForm1
Dim oBM As Bookmark
With ofrm
With .ListBox1
For Each oBM In ActiveDocument.Bookmarks
.AddItem oBM.Name
Next oBM
.ListIndex = -1
End With
.Show
If .Tag = 1 Then
ActiveDocument.Bookmarks(.ListBox1.Text).Range.Select
End If
End With
lbl_Exit:
Exit Sub
End Sub
and in the userform code
Option Explicit

Private Sub CommandButton1_Click()
If Me.ListBox1.ListIndex < 0 Then
MsgBox "No Bookmark selected"
Me.ListBox1.SetFocus
GoTo lbl_Exit
End If
Me.Hide
Me.Tag = 1
lbl_Exit:
Exit Sub
End Sub

Private Sub CommandButton2_Click()
Me.Hide
Me.Tag = 0
lbl_Exit:
Exit Sub
End Sub