View Single Post
 
Old 05-26-2018, 01:56 AM
THH4929 THH4929 is offline Windows 10 Office 2016
Novice
 
Join Date: May 2018
Posts: 3
THH4929 is on a distinguished road
Default Replacing text held in all bookmarks

Hello all,


I'm new to the whole VBA in word, and am struggling to resolve this issue!!!


What I am trying to achieve is to have a doc with various bookmarks, when a script is run, vba systematically goes through all bookmarks, requests user input via an inputbox, and then replaces the text with the user determined text.


This is what I have so far;


Code:
Sub UpdateBookmarks()

Dim bmInput As String

For Each bm In ActiveDocument.Bookmarks
    bmInput = InputBox("Please enter " & bm.Name)
    UpdateBM ActiveDocument, bm.Name, bmInput
Next

End Sub

Sub UpdateBM(ByRef bmDoc As Document, bmName As String, bmContent As String)
Dim bmRng As Word.Range
    Set bmRng = bmDoc.Bookmarks(bmName).Range
    bmRng.Text = bmContent
End Sub
However, when I run the code, it inserts the user input at the beginning of the text already present in the bookmark, rather than replacing it.


Please can anyone help??
Many thanks all
Reply With Quote