Consulting

Results 1 to 2 of 2

Thread: Update bookmark text

  1. #1

    Update bookmark text

    Hi,
    I have several bookmarks and corresponding names and texts as follows:

    [bookmark name : bookmark text]

    Project_1_bookmark1:Project1_Nov_2018
    Project_2_bookmark2:Project2_Jan_2019
    Project_3_bookmark3:Project3_Mar_2019

    I need to change all the bookmark texts about year into 2020

    i.e.

    Project_1_bookmark1:Project1_Nov_2020
    Project_2_bookmark2:Project2_Jan_2020
    Project_3_bookmark3:Project3_Mar_2020

    How can I do that?

    Thanks in advance!

  2. #2
    There are probably several ways to approach this - Based on your message the following should work for the three examples

    Sub UpdateBookmarks()
    'Graham Mayor - http://www.gmayor.com - Last updated - 30/04/2017
    Dim i As Long
    Dim oBM As Bookmark
    Dim sText As String
    Dim sName As String
        On Error GoTo err_Handler
        For i = 1 To 3
            For Each oBM In ActiveDocument.Bookmarks
                If oBM.Name = "Project_" & i & "_bookmark" & i Then
                    sName = oBM.Name
                    sText = oBM.Range.Text
                    sText = Left(sText, Len(sText) - 4) & "2020"
                    FillBM sName, sText
                    Exit For
                End If
            Next oBM
        Next i
    lbl_Exit:
        Set oBM = Nothing
        Exit Sub
    err_Handler:
        MsgBox Err.Number & vbCr & Err.Description
        Err.Clear
        GoTo lbl_Exit
    End Sub
    
    Public Sub FillBM(strBMName As String, strValue As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strBMName).Range
            orng.Text = strValue
            orng.Bookmarks.Add strBMName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    If the bookmarks and their contents are not as well defined in reality as the examples then see http://www.gmayor.com/BookmarkandVariableEditor.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •