-
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!
-
There are probably several ways to approach this - Based on your message the following should work for the three examples
Code:
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