Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2018, 01:56 AM
THH4929 THH4929 is offline Replacing text held in all bookmarks Windows 10 Replacing text held in all bookmarks Office 2016
Novice
Replacing text held in all bookmarks
 
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
  #2  
Old 05-26-2018, 02:41 AM
macropod's Avatar
macropod macropod is offline Replacing text held in all bookmarks Windows 7 64bit Replacing text held in all bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-26-2018, 04:56 AM
gmayor's Avatar
gmayor gmayor is offline Replacing text held in all bookmarks Windows 10 Replacing text held in all bookmarks Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by THH4929 View Post
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.
See the FillBM function (and other handy functions) at https://www.gmayor.com/useful_vba_functions.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 05-27-2018, 06:35 AM
gmaxey gmaxey is offline Replacing text held in all bookmarks Windows 7 32bit Replacing text held in all bookmarks Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 06-01-2018, 12:14 PM
THH4929 THH4929 is offline Replacing text held in all bookmarks Windows 10 Replacing text held in all bookmarks Office 2016
Novice
Replacing text held in all bookmarks
 
Join Date: May 2018
Posts: 3
THH4929 is on a distinguished road
Default

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.
Attached Files
File Type: docm bookmarks.docm (21.3 KB, 9 views)
Reply With Quote
  #6  
Old 06-01-2018, 03:16 PM
macropod's Avatar
macropod macropod is offline Replacing text held in all bookmarks Windows 7 64bit Replacing text held in all bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 06-02-2018, 04:29 AM
gmaxey gmaxey is offline Replacing text held in all bookmarks Windows 7 32bit Replacing text held in all bookmarks Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing text held in all bookmarks 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
Replacing text held in all bookmarks Problem replacing text. piritzo Word 2 06-22-2013 12:50 AM
Replacing text held in all bookmarks 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft