Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-12-2021, 10:26 AM
Javi78503 Javi78503 is offline Find and replace help for a newbie Windows 10 Find and replace help for a newbie Office 2019
Novice
Find and replace help for a newbie
 
Join Date: Jul 2021
Posts: 12
Javi78503 is on a distinguished road
Default Find and replace help for a newbie

Hey guys, quick rundown of what i'm trying to accomplish. I've created a form that updates bookmarks (maybe not the best way but it gets the job done so far) What I'm trying to make is a field that updates as you type it in. I can get it to do that easy enough, but my struggle is trying to get it to replace the old text with new text if you change the textbox value. this is what I have so far and I can't seem to figure out how to find and delete the old text..or replace it..

Code:
Private Sub PNBox_AfterUpdate()

If oldPN <> "" Then

With ActiveDocument.Content.Find
 .Text = oldPN
 .Replacement.ClearFormatting
 .Replacement.Text = ""
 .Execute Replace:=wdReplaceAll, Forward:=True, _
 Wrap:=wdFindContinue
End With
End If


ActiveDocument.Bookmarks("bmPN").Select
Selection.Text = StrConv(Me.PNBox.Value, vbProperCase)

oldPN = Me.PNBox.Value
End Sub
I set oldPN as a string at the top of the page and am trying to:

1. check if oldPN is still null
2. if not, find and delete all instances of oldPN in the form (should be at bookmarks)
3. update bookmarks with new textbox value
4. update oldPN to be the newly entered value so it can have something to compare if it changes again.

currently i'm just getting the new value added in front of the old value into an ever growing string.

Thanks in advance for any help!
Reply With Quote
  #2  
Old 08-12-2021, 04:28 PM
Guessed's Avatar
Guessed Guessed is online now Find and replace help for a newbie Windows 10 Find and replace help for a newbie Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The code you posted looks problematic as you could delete content all over the document if it gets a mention in oldPN. In my opinion, bookmarks are too unwieldy to use nowdays - especially where there are more than one instance you need to find - you should be using Content Controls instead.

For instance, if the CCs had a title of PNBox you could update the content in each with
Code:
Sub CCupdate()
  Dim aCC As ContentControl
  For Each aCC In ActiveDocument.SelectContentControlsByTitle("PNBox")
    aCC.Range.Text = "hello"
  Next aCC
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-12-2021, 10:10 PM
gmayor's Avatar
gmayor gmayor is offline Find and replace help for a newbie Windows 10 Find and replace help for a newbie Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

Even simpler, if you employ https://www.gmayor.com/insert_content_control_addin.htm to insert a rich text content control with the mapped option checked. You can copy and paste that control wherever you wish in your document and whatever you enter in one of them will be repeated in the others without the need for macros.
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you use the find and replace tool to find dates and times in Excel 2013? Jules90 Excel 3 04-14-2020 07:40 PM
In Find and Replace, can Word stop after each Replace? wardw Word 1 06-08-2017 02:47 PM
Find and replace help for a newbie Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
Find and replace help for a newbie Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Find and replace help for a newbie Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:49 PM.


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