#1
|
|||
|
|||
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 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! |
#2
|
||||
|
||||
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 |
#3
|
||||
|
||||
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 |
|
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 what box in Find and replace limits the length of a search term | Hoxton118 | Word VBA | 7 | 06-10-2014 05:05 AM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |
Help with find and replace or query and replace | shabbaranks | Excel | 4 | 03-19-2011 08:38 AM |