Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
Cristina02
Femeie
23 ani
Arges
cauta Barbat
26 - 80 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [WORD] Create Bookmark List for Current Document [VBA] Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
Source:

If your document contains several bookmarks, referring to them will become tedious. Offering a macro that displays a list of bookmarks that the user can then select is more efficient. Listing A includes several sub and function procedures that your users can run as a macro. It will display a UserForm that lists all of the document's bookmarks. The user simply selects a bookmark from the list to select that bookmark. I recommend that you add the macro to the Quick Access Toolbar (QAT) for even quicker access. That's where Listing B comes in. While in the VBA, insert a module and add this short procedure.

Don't attempt to copy and paste the code from this article into the UserForm's module because the VBE will object to web characters. Instead, download the demonstration .docm file or frmSelectBookmark.frm.

If you want to import the UserForm yourself, you can by using the instructions that follow:

1. Press [Alt]+[F11] to open the VBE.
2. Select UserForm from the Insert menu. Name it frmSelectBookmark.
3. Using the image below as a guide, add the following controls:

Text box ---- txtBookmarkName
List box ---- lstBookmarks
Command button ---- cmdGoTo
Command button ---- cmdCancel



4. Double-click the UserForm to open its module and enter the code in Listing A. (If you copy the code from the web page into a Word document and then copy that code into the module, it might work, but I can't guarantee it.)
5. Insert a module by selecting Module from the Insert menu.
6. Enter the code in Listing B.
7. Return to the document and save it as a macro-enabled document (if necessary).

Listing A


Option Explicit
Private 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


Listing B


Public Sub ShowBookmarksSelectionForm()
    frmSelectBookmark.Show
End Sub


Modificat de TRaP (acum 4 ani)


pus acum 4 ani
   
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
Getting the Names of Defined Bookmarks

pus acum 4 ani
   
Pagini: 1  

Mergi la