Posts Tagged ‘wildcards’

h1

Word: Find and replace multiple spaces after punctuation

December 21, 2016

You receive a document that has multiple spaces after standard punctuation — periods, commas, semicolons, colons, question marks, and exclamation marks. Sometimes the author used two spaces, sometimes three, sometimes five!

How to fix it?

Well, you can run several find/replace routines but as the number of spaces is unknown and as there are many types of punctuation, that could take quite a lot of time. Instead, you can use a wildcard find/replace routine to find them all at once, keep the punctuation, and replace the unknown number of spaces with a single space. Here’s how (for your own peace of mind, test this on a COPY of your document first):

  1. Press Ctrl+H to open the Find and Replace dialog box.
  2. Click the More button to show more find/replace options.
  3. Select the Use wildcards checkbox.
  4. In the Find what field, type: ([,.;:\?\!])( {2,9})
    NOTE: There’s a single space before the {2 — make sure you include that. To be safe, copy the ‘code’ in this step, and paste it into your Find what field.
  5. In the Replace with field, type: \1 followed immediately by a single space.
  6. Click Find Next to find the first instance, then Replace to replace the multiple spaces with a single space.
  7. Repeat step 6 as many times as you need to be confident that it’s finding the right things. Once you’re confident, click Replace All to run through the whole document and fix all instances.

Use wildcards to find and replace multiple spaces after defined punctation

Explanation for how this works:

  • ([,.;:\?\!]) looks for any of the listed punctuation characters. Question and exclamation marks are special cases and need to be ‘escaped’ with a \. Because you’re using wildcards, you need to surround the text you want to find in parentheses. This string defines the first section of the Find.
  • ( {2-9}) looks for a space followed by two or more spaces, up to 9 spaces (you can put whatever numbers you like inside the curly braces — if you think you might have some instances of punctuation followed by 15 spaces, then change these numbers to {2-20}, for example. Again, this section is surrounded by parentheses to define it as a separate section.
  • \1 replaces the first part of the wildcard string with itself. In other words, the punctuation character found is replaced with itself, so no change apparently occurs.
  • The space after \1 replaces the multiple spaces found in the second part of the wildcard string with a single space.
h1

Word: Find and highlight multiple words at once

November 6, 2015

This is a variation on the multiple find/replace macro covered here: https://cybertext.wordpress.com/2015/03/03/word-macro-to-run-multiple-wildcard-find-and-replace-routines/. I suggest you read that post first (and test it) before attempting this one. This post assumes you can already do what’s in that earlier post.

Scenario

The scenario for this one is a little different — the author wanted to find various words in a document and highlight them for probable change. As there were MANY documents she had to process, she needed an easy way to find these words and highlight them. She didn’t want to change them at this stage. The words related to project names, company names, facility names, document number prefixes, etc. One project’s documents were to be the basis of a set of documents for another project in the same company, so one of the tasks was to ‘personalize’ the copies of the original project’s documents with the names used by the new project.

Solution

Although there may be several ways you can do this, I decided to stick with what I already knew. I figured that using a variation of the ReplaceTableWithList macro (as discussed here: https://cybertext.wordpress.com/2015/03/03/word-macro-to-run-multiple-wildcard-find-and-replace-routines/) and a new table called by that macro variation should solve it. And it did.

Step 1: Create your find/replace table

  1. Start a new Word document, and create a two-column table in it.
  2. In the left column, type in the words/phrases you want to find, each on a different row.
  3. In the right column, type \1. (This means that the wildcard find/replace will replace what was found with itself — remember, you’re only trying to identify the words that *may* need changing at this point, not changing them.)
  4. Save the document to this folder, noting the name of the file as you’ll need that later:
    C:\Users\<your_username>\AppData\Roaming\Microsoft\Word\STARTUP.

Step 2: Set up the macro to work with the table

Add the macro below to an existing document, or better, an existing template, or even better, a central macros template that loads whenever you open Word.

Once you’ve added it, change the line that starts with sFname and has the file path — that path points to MY file on MY computer. You need to change it to YOUR file name and YOUR file path.

NOTE: Copy all the code below to the clipboard — it goes off the page, so don’t type it out as you’ll miss some of it or could make a typo.

Sub ReplaceFromTableListName()
' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim sFname As String
'Change the path in the line below to reflect the name and path of the table document
sFname = "C:\Users\rhonda\AppData\Roaming\Microsoft\Word\STARTUP\find_and_replace_routines_names_macro.docx"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)

' Make sure highlight is set to the colour you want, e.g. wdYellow, wdBrightGreen, wdPink, wdTurquoise
Options.DefaultHighlightColorIndex = wdTurquoise

For i = 1 To oTable.Rows.Count
 Set oRng = oDoc.Range
 Set rFindText = oTable.Cell(i, 1).Range
 rFindText.End = rFindText.End - 1
 Set rReplacement = oTable.Cell(i, 2).Range
 rReplacement.End = rReplacement.End - 1
 Selection.HomeKey wdStory
 With oRng.Find
 .ClearFormatting
 .Replacement.ClearFormatting
 .MatchWildcards = True
 .Text = rFindText.Text
 .Replacement.Text = rReplacement.Text
 .Replacement.Highlight = True
 .Forward = True
 .Wrap = wdFindContinue
 .Execute Replace:=wdReplaceAll
 End With
Next i
oChanges.Close wdDoNotSaveChanges
End Sub

Step 3: Test that it works

After setting up the ReplaceFromTableListName macro (above), run it on a test document — copy an existing document and test on the copy to make sure you don’t mess up anything.

NOTES:

  • If you get an error message, check that you have the correct file name and path in the macro, AND check that your Word document containing the table that’s called by the macro has no empty rows.
  • If none of the words in your table get highlighted in your document on the first pass, select a highlight colour from the Home tab as though you were going to highlight manually, then run the macro again.
  • If some words are missed, check the table containing them — if a word has an initial capital in the table, but not in the document you are searching (e.g. in a URL), then the macro won’t highlight it. For words that could be capitalized in various ways, either add new lines for each variation, OR search for the part of the word you know won’t be capitalized (e.g. if you were searching for ‘Amazon’, then change the search term in the first column to ‘mazon’ to pick up the word whether it has an initial cap or not).
  • Some areas of your document won’t get highlighted, even if they contain the words you’re looking for — e.g. headers and footers, text boxes. You’ll have to check for these manually.

My author was VERY happy — she had something that only took a few seconds to run and highlighted all the various words she needed to look for.

[Links last checked November 2015]

h1

Word: Find and Replace features summary

August 21, 2015

Word’s Find and Replace is a very handy tool, with lots of powerful features that many people don’t use or don’t know exist.

I could have written a long piece summarizing these features, but someone has already done so: http://libroediting.com/2014/01/23/find-and-replace-in-word-2007-2010-and-2013-2-advanced-find/. This web page is well worth bookmarking, especially if you don’t use Find/Replace often and need to be reminded of some of its features.

Update October 2015: For an even more comprehensive look at all Find/Replace options (other than Wildcards), see: https://americaneditor.wordpress.com/2015/09/30/lyonizing-word-but-wait-theres-more/

For those wanting to know more, I’ve written many blog posts on specific Find and Replace actions in Word, including using wildcards (a very powerful find/replace option):

[Links last checked August 2015]

h1

Word: Find words with numerals and delete the numeral

July 29, 2015

Lesley, a transcriptionist from the UK, emailed me with her problem:

…although [I’m] a fast typist I do make mistakes. One of my common ones is that when I’m typing quickly I can hit a number key so a word can come out like tr4st for example or what4ever.

She wanted to know if there was a wildcard find/replace for [a-z][0-9] that would solve her problem.

Well, yes there is, but Word already offers some standard solutions to identify and fix these situations (Method 1), which may mean you don’t need the find/replace solution (Method 2).

Method 1: Use Word’s existing settings and functions

There’s a setting to ignore numbers in words when doing a spell check. If you turn that off and turn on check spelling as you type, AND run a final spell check, that may solve the problem as the spell check function will find them.

  1. Go to File > Options > Proofing.
  2. Make sure the Ignore words that contain numbers checkbox is clear (unselected).
  3. For added checks, turn on Check spelling as you type (also in that Proofing area).
  4. Don’t forget to run the spellchecker when you’ve finished your document.

Method 2: Use a wildcard find and replace

If you’d rather use a find/replace routine to find words with a single numeral surrounded by lower case letters (as in Lesley’s examples above), then follow the steps below.

NOTE: This find string will NOT find occurrences of upper case letters before or after the numeral, more than two numerals, or a numeral at the start or end of a word (e.g. 4trust, whatever4).

  1. Press Ctrl+H to open the Find and Replace dialog box on the Replace tab.
  2. Click More.
  3. Select the Use wildcards check box.
  4. In the Find What field, type: ([a-z])([0-9])([a-z]) (This string looks for a lower case letter immediately followed by a single number, immediately followed by a lower case letter. There are NO spaces in this string.)
  5. In the Replace With field, type \1\3 (You’re replacing the first and third found elements [i.e. the lower case letters] with themselves, and not replacing the number, thus deleting it. NOTE: There are NO spaces in this replace string.)
  6. Click Find Next to find the first instance of a number inside a word.
  7. Assuming the number is found correctly and it’s what you want to delete, click Replace.
  8. Repeat steps 6 and 7 for all instances.

Warning: You could do Replace All but you have to be ABSOLUTELY certain you aren’t replacing something you shouldn’t. Replace All is very powerful and makes global changes… You have been warned!

This is what your Find and Replace dialog box should look like:

numeral_FR

h1

Word: Change subscripted numerals to normal and surround with square brackets

July 22, 2015

On another blog post (https://cybertext.wordpress.com/2009/08/06/word-replace-text-containing-superscript-and-subscript-characters/), Bill asked:

All of my subscripts are consecutive numbers. I need to change all of them to normal size and with brackets around each consecutive number. Any ideas how I can do this for a large document?

You can do this using wildcards in the find and replace window, BUT I couldn’t find an easy way to get numbers greater than one numeral, so you may have to run two (for tens) or three (for hundreds) instances to get them all. Despite the apparent length of the steps below, it shouldn’t take very long — a few minutes at most.

(For others reading this, yes, I tried ([0-9]@) but for some reason it didn’t work — when the replace occurred, it replaced each number individually and surrounded each numeral with its own set of square brackets, which is not what was wanted. Update Aug 2015: I’ve found a simpler solution! And as a result have deleted all the complex steps I originally documented in this post.)

As the consecutive numbering that Bill talked about is irrelevant to my solution, I’ll ignore it.

Because you are potentially making global changes to your document, I strongly suggest you work on a copy of it, not the original, until you are satisfied that it works as you expect it to.

Find and replace one or more subscripted numbers

  1. Press Ctrl+H to open the Find and Replace dialog box on the Replace tab.
  2. Make sure your cursor is in the Find what field.
  3. Click More.
  4. Select the Use wildcards check box.
  5. Click Format.
  6. Click Font.
  7. Select the Subscript check box until it becomes a check mark.
  8. Click OK to close the Font dialog box. You should have Use wildcards and Subscript listed below the Find What field.
  9. In the Find What field, type: ([0-9]{1,}) (NO spaces).
  10. In the Replace With field, type [\1] (you’re putting square brackets around what you’re replacing, and you’re replacing the found element with itself [that’s the \1 bit]).
  11. While your cursor is still in the Replace With field, click Format again.
  12. Click Font.
  13. Select the Subscript check box until it becomes blank, then click OK.
  14. Click Find Next to find the first subscripted numeral.
  15. Assuming the number is found correctly and it’s what you want to change, click Replace.
  16. Repeat steps 14 and 15 for all the other 4-digit numerals.

Warning: You could do Replace All but you have to be ABSOLUTELY certain you aren’t replacing something you shouldn’t. Replace All is very powerful and makes global changes… You have been warned!

Your Find and Replace dialog box should look like this:

subscript_FR_01a

h1

Word: Wildcard find and replace for numbers and trailing punctuation

July 14, 2015

On another post on this blog, Michael commented that he had a situation where he needed to run find and replace routines (possibly using wildcards) on hundreds of documents to convert MANUALLY entered step numbers (with various trailing punctuation) to a common format ready for conversion into another type of document.

He wanted to convert numbered steps like 1), 2), 3), etc. (each on a new line) to (1), (2), (3), etc. (also on separate lines). And convert numbered steps like (1), (2), (3), etc. (each on a new line) to 1., 2., 3., etc. (also on separate lines).

Notes and assumptions:

  • These steps ONLY work for manually entered numbered steps, NOT automatic numbering in Word.
  • I have assumed that each numbered item starts a new paragraph.
  • Before you test this on your document, make a copy of the document and work on that until you are comfortable with what you have to do, and until you are satisfied that these steps are what you want.
  • Be very careful with the ‘Replace All’ command — you may inadvertently replace something you don’t want to replace. Only if you’re 100% sure there are no other uses of a number and the relevant trailing punctuation would you use ‘Replace All’. If you’re not sure, use ‘Replace’ only. Yes, this will mean clicking ‘Replace’ for each one, but until you are confident that you won’t replace something you shouldn’t, it’s a safer option.

Example 1: Replace 1) etc. with (1)

  1. Press Ctrl+H to open the Find and Replace dialog.
  2. Click More, then select the Use wildcards check box.
  3. In Find What, type: (^013)([0-9]@)([\)])
  4. In Replace With, type: \1(\2\3

The three elements of the Find are:

  1. (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  2. ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283,
  3. (\)) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash ‘\‘ (also known as an ‘escape’ character) before the ).

There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, PLEASE get rid of any preceding spaces otherwise it won’t work (yes, I know this because it caught me out too!).

The four elements of the Replace are:

  1. \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  2. (  — Tells Word to add an opening parenthesis before the next element (the number).
  3. \2 — Tells Word to replace the second element of the Find with the same text as what was found (the numerals).
  4. \3 — Tells Word to replace the third element of the Find with what was in the Find (the closing parenthesis).

Example 2: Replace (1) etc. with 1.

  1. Press Ctrl+H to open the Find and Replace dialog.
  2. Click More, then select the Use wildcards check box.
  3. In Find What, type: (^013)([\(])([0-9]@)(\))
  4. In Replace With, type: \1\3.

What the find and replace ‘codes’ mean:

The four elements of the Find are:

  1. (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  2. ([\(]) — You need to find a specific character (the opening parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash ‘\‘ (also known as an ‘escape’ character) before the (, AND square brackets surrounding this string (otherwise, it won’t work).
  3. ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283,
  4. (\)) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash ‘\‘ (also known as an ‘escape’ character) before the ).

There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, get rid of any preceding spaces otherwise it won’t work .

The three elements of the Replace are:

  1. \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  2. \3 — Tells Word to replace the third element of the Find with the same text as what was found (the numerals).
  3. . — Tells Word to add a period (full stop) after the third element of the Find.

Michael: I hope this solves your problem. Donations to keeping this blog ad-free gratefully accepted (see the link at the top right of the page).

Update: Michael also wanted to know how to revise these find Strings if the lists used lower case letters — e.g. a), b) or (a), (b), etc. instead of numerals. In that case you’d substitute the [0-9]@ in both examples above with [a-z]. If the letters might include upper case letters, then you’d use [A-z]. Don’t forget to omit the @!

h1

Word: Macro to run multiple wildcard find and replace routines

March 3, 2015

After some digging around, I’ve found a way to save myself hours of running separate find and replace routines using wildcards in Microsoft Word.

The routines I commonly use already save me time, but now I’ve got one macro that runs them all at once — currently 110 of them that are just for replacing an ordinary space with a non-breaking space!

I didn’t write the macro — that honor goes to Microsoft Word MVPs Graham Mayor who wrote the initial macro, and Doug Robbins who helped me tweak it to do what I wanted it to do. Details are here: http://answers.microsoft.com/en-us/office/forum/office_2003-word/macro-for-performing-more-than-one-find-and/bd931bf7-5ebe-4650-924c-d15c9512129c?page=1

NOTE: EditTools software (http://wordsnsync.com/edittools.php) has a neat interface for setting up your wildcard find and replace routines, along with a facility to set up a script to run them all, and I use that on my own PC. However, I’m not allowed to install that software on my work PC (long story…), so I wanted to figure out a way to emulate what an EditTools script does by creating a macro to achieve the same end.

Before doing these steps, you need to be familiar with:

  • accessing the VBA area of Word
  • using wildcards to find and replace in Word.

I won’t cover either of those things in this post.

Step 1: Create a table of your wildcard find/replace routines

The first stage in getting this to work is to create a 2-column table in Word, listing what you want to find in the left column and what you want to replace it with in the right column. Use one row for each find/replace routine. Use the same syntax as you would for a wildcard find/replace.

This table will take a while to set up. You should test each wildcard find/replace routine on a document BEFORE you add it to the list, just to make sure it works correctly and doesn’t mess up anything else. These find/replace routines will all be done as ‘replace all’, so you will NOT have the opportunity to accept some and reject others. Testing is crucial to make sure you don’t replace something you shouldn’t.

In the example below, I’ve set up routines for replacing a standard space between a number and a month of the year with a non-breaking space (^s) between those two elements, as well as replacing a standard space between a number and a unit of measure (e.g. lux, nm) or a word that often follows a number in the documents I work on (e.g. fish, indiv [for ‘individual’ or ‘individuals’). As I said, I have some 110 of these…

widlcard_fr_macro01

Once you’ve created your table, save it to a location that your macro can pick it up from easily — I suggest somewhere on your local drive, not a network location (unless you intend sharing it with others). I put mine in my Word > STARTUP folder, which is where I keep my macros.dotm file.

Step 2: Set up the macro to work with the table

Add the macro below to an existing document, or better, an existing template, or even better, a central macros template that loads whenever you open Word.

Once you’ve added it, change the line that starts with sFname and has the file path — that path points to MY file on MY computer. You need to change it to YOUR file name and YOUR file path.

NOTE: Copy all the code below to the clipboard — it goes off the page, so don’t type it out as you’ll miss some of it or could make a typo.

Sub ReplaceFromTableList()
' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010
 Dim oChanges As Document, oDoc As Document
 Dim oTable As Table
 Dim oRng As Range
 Dim rFindText As Range, rReplacement As Range
 Dim i As Long
 Dim sFname As String
 'Change the path in the line below to reflect the name and path of the table document
 sFname = "C:\Users\rhonda\AppData\Roaming\Microsoft\Word\STARTUP\find_and_replace_routines_macro.docx"
 Set oDoc = ActiveDocument
 Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
 Set oTable = oChanges.Tables(1)
 For i = 1 To oTable.Rows.Count
     Set oRng = oDoc.Range
     Set rFindText = oTable.Cell(i, 1).Range
     rFindText.End = rFindText.End - 1
     Set rReplacement = oTable.Cell(i, 2).Range
     rReplacement.End = rReplacement.End - 1
     Selection.HomeKey wdStory
     With oRng.Find
             .ClearFormatting
             .Replacement.ClearFormatting
             .MatchWildcards = True
             .Text = rFindText.Text
             .Replacement.Text = rReplacement.Text
             .Forward = True
             .Wrap = wdFindContinue
             .Execute Replace:=wdReplaceAll
       End With
 Next i
 oChanges.Close wdDoNotSaveChanges
End Sub

Step 3: Test that it works

After setting up the macro, run it on a test document — copy an existing document and test on the copy to make sure you don’t mess up anything.

As my file had 110 find/replace routines for changing some normal spaces to non-breaking spaces, I needed to test it on a document that had many normal spaces for the elements I wanted to change to non-breaking spaces. I opened a copy of an existing 400p Word document, used Word’s normal find/replace to replace all existing non-breaking spaces with a normal space (some 3000 of them in that document I tested it on!). Then I ran the ReplaceFromTableList macro to see if it worked.

It did. Beautifully. And it took about 3 minutes to run ALL those routines on that 400p document. The end result was a document that had more than 5000 non-breaking spaces added to it (I checked the number by taking them all out again and replacing them with normal spaces!).

One 400p Word document, 110 separate find/replace routines. In 3 minutes. In one macro.

That’s gotta be a win for automating tedious, routine tasks! Now, to think about what other find/replace routines I can add to the table…

[Links last checked March 2015]