#1
|
|||
|
|||
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 Please can anyone help?? Many thanks all |
#2
|
||||
|
||||
Most likely the issue is that the text you're seeking to update isn't actually bookmarked - even your own code doesn't ensure that. You need to insert:
bmDoc.Bookmarks.Add bmName, bmRng after: bmRng.Text = bmContent
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
Quote:
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
or this:
Code:
Sub UpdateBookmarks() Dim lngIndex As Long Dim oRng As Range Dim strName As String With ActiveDocument For lngIndex = 1 To .Bookmarks.Count Set oRng = .Bookmarks(lngIndex).Range strName = .Bookmarks(lngIndex).Name oRng.Text = InputBox("Please enter " & strName) .Bookmarks.Add strName, oRng Next End With lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Thank you for your replies, however the suggestions from Macropod only seems to loop the first BM, similar to that of Gmayor, I have attached a copy of the test word doc for additional review.
gmaxey suggestion seems to do the trick, however, is there a way of sequencing the bookmarks to the user completes the details in a logical manor, ie. CompName, CompAddress, CompTel, rather than the character sort of CompAddress, COmpName, CompTel. Many thanks once again for your help on this. |
#6
|
||||
|
||||
The problem isn't with anything that Graham or I posted but with the the wrong kind of loop being used in your own code. Try:
Code:
Sub UpdateBookmarks() Dim i As Long With ActiveDocument For i = 1 To .Bookmarks.Count With .Bookmarks(i) Call UpdateBM(.Name, InputBox("Please enter " & .Name)) End With Next End With End Sub Sub UpdateBM(bmName As String, bmContent As String) Dim bmRng As Word.Range With ActiveDocument Set bmRng = .Bookmarks(bmName).Range bmRng.Text = bmContent .Bookmarks.Add bmName, bmRng End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
If you name your bookmarks with a logical processing manner in mind then yes.
Code:
Sub UpdateBookmarks() Dim lngIndex As Long, lngPart As Long Dim oRng As Range Dim strName As String, strDisplay As String Dim arrParts() As String With ActiveDocument For lngIndex = 1 To .Bookmarks.Count Set oRng = .Bookmarks(lngIndex).Range strName = .Bookmarks(lngIndex).Name arrParts = Split(strName, "_") If UBound(arrParts) > 0 Then 'For processing bookmarks names using following convention: _ A_Complany_Name, B_Company_Address, etc. strDisplay = vbNullString For lngPart = 1 To UBound(arrParts) strDisplay = strDisplay & " " & arrParts(lngPart) Next oRng.Text = InputBox("Please enter " & strDisplay) Else oRng.Text = InputBox("Please enter " & strName) End If .Bookmarks.Add strName, oRng Next End With lbl_Exit: Exit Sub End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Tracking Stock Shares Held/Sold & Current Value | CCITE140 | Excel | 1 | 11-30-2015 07:42 AM |
Replacing the 2nd or 3rd instance of Text | wdillon2 | Word VBA | 2 | 05-13-2015 10:21 PM |
Problem replacing text. | piritzo | Word | 2 | 06-22-2013 12:50 AM |
Help with replacing text with wildcards | sbatson5 | Word | 2 | 04-13-2012 03:49 AM |
Replacing / editting text | LisaC | Word | 0 | 02-25-2010 03:40 AM |