Using Find and Replace

I am an architect, and I have a standard set of notes I use when building houses. Sometimes I use these notes for a new house, sometimes, I use them for an addition onto an existing house. While most of the notes apply to both new construction and additions, there are some notes that refer only to new construction and some refer only to additions. Right now, I have to manually edit the text to remove the notes that don't apply. I am hoping to use find and replace to make my task easier. For example, here is a note that I use right now:

"Provide waterproofing on [new] foundation walls [only]."

For new construction, I would delete the "[new]" to make the sentence read "Provide waterproofing on foundation walls."

For additions, I would keep use the word "new" and "only" and just delete the brackets so the sentence would read "Provide waterproofing on new foundation walls only."

I thought I might be able to tag each bracketed comment so I could search and remove the brackets on the type of project I am writing for. For example, if I wrote my basic note as "Provide waterproofing on [ADDnew] foundation walls [ADDonly]", and wanted to edit it for an addition, I would search for "[ADD*]" and replace it with the word or words represented by the asterisk. If it was for new construction, I would just delete all the notes relating to additions. That would save me time editing the whole document for each of these little items (of which there are a lot).

I've read through the find and replace section, and can't find anything that would address this, but maybe someone knows a good way to be able to do this. I'm using the brackets, but it may be necessary to replace them with something else.

Any help or suggestions would be appreciated. Thank you!

Answer
Answer

I'll start by recommending that you don't use find/replace this way at all. Instead, store separate AutoText building blocks for two versions of each passage, one for new construction and one for additions. Name the building blocks in a logical way so you can remember them or so they'll be grouped logically in the AutoText gallery (which you can place on the Quick Access Toolbar). With this sort of setup, you can be confident that each document has the correct wording.

---------

If you want to continue with find/replace, what you need is the "wildcard" search. In the Replace dialog, click the More button and check the box for "Use wildcards". This uses a rather arcane syntax that's thoroughly covered at http://www.gmayor.com/replace_using_wildcards.htm.

Supposing you have a passage such as

Provide waterproofing on [ADDnew] foundation walls [ADDonly].

For an addition, you need to put this expression in the Find What box of the Replace dialog:

\[ADD([A-Za-z ]@)\]

and put this in the Replace With box:

\1

Then click the Replace All button. The part of the find expression in parentheses will match whatever combination of upper and lower case letters and spaces appears between the ADD and the closing bracket, and the \1 in the replace expression represents that string.

For new construction, use the same expression in the Find What box, and leave the Replace With box empty. After that, you'll have to replace two spaces with one space, and replace space-period with just a period (and the same for any other punctuation that might have followed the expression you removed).

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

I agree with Jay that AutoText entries are the way forward, but in conjunction with autotext fields in the document.

If you name the autotext entries using a particular and unique convention then you can use the replace function in a macro to swap 'New build' texts for 'Additional build' texts. In the following example I would name all new build Texts 'NEWname' and all additional build texts 'ADDname', so for the example you quoted


Provide waterproofing on new foundation walls only.

could be saved as 'ADDwaterproofing'

and

Provide waterproofing on foundation walls.

could be saved as 'NEWwaterproofing'

Save your document as a macro enabled template and create as many autotexts as required for your document and save them in the template. The texts can be as large or as small as required to complete the task. I would caution against including user editable texts within the autotext entries as this creates other issues not addressed in the example macro.

Insert Autotext fields in the document as required to place the autotexts for a new build.


Add the following macros (
http://www.gmayor.com/installing_macro.htm ) to the template:

When you create a new document from the template you will see a prompt "Is this document for a New Build?" Answer Yes and the macro will insert all the texts for a new build. Answer No and it will insert all the texts for additional builds. You may also Cancel.

The other two macros allow you to change the document.

Option Explicit

Sub ChangeAutotext(bNew As Boolean)
Dim oStory As Range
Dim oFld As Field
    For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
            If oFld.Type = wdFieldAutoText Then
                oFld.Locked = False
                If bNew Then
                    oFld.Code.Text = Replace(oFld.Code, "ADD", "NEW")
                Else
                    oFld.Code.Text = Replace(oFld.Code, "NEW", "ADD")
                End If
                oFld.Update
                oFld.Locked = True
                Exit For
            End If
        Next oFld
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For Each oFld In oStory.Fields
                    If oFld.Type = wdFieldAutoText Then
                        oFld.Locked = False
                        If bNew Then
                            oFld.Code.Text = Replace(oFld.Code, "ADD", "NEW")
                        Else
                            oFld.Code.Text = Replace(oFld.Code, "NEW", "ADD")
                        End If
                        oFld.Update
                        oFld.Locked = True
                        Exit For
                    End If
                Next oFld
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub

Sub MacroNew()
    ChangeAutotext True
End Sub

Sub MacroAdd()
    ChangeAutotext False
End Sub

Sub AutoNew()
Dim i As Integer
    i = MsgBox("Is this document for a New Build?", vbYesNoCancel)
    Select Case i
        Case 6: ChangeAutotext True
        Case 7: ChangeAutotext False
        Case Else
    End Select
lbl_Exit:
    Exit Sub
End Sub


Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 14, 2025 Views 185 Applies to: