Posts Tagged ‘find and replace’

h1

Word: Insert a degree symbol

September 22, 2018

There’s a special character for a degree, so don’t make the mistake of superscripting a lower case ‘o’. Instead, use one of these methods to insert a proper degree symbol in Microsoft Word:

  • If you have a separate number pad, then press Alt+0176 (press and hold the Alt key while you type 0176)
  • For any keyboard with or without a number pad, press Ctrl+Shift+@.
  • Go to the Insert tab > Symbol — the degree symbol is character code = 00B0, Unicode (hex)
  • If you have Math AutoCorrect turned on, then type \degree (to turn on Math AutoCorrect: File > Options > Proofing > AutoCorrect Options > Math AutoCorrect tab).

If you have a lot of superscripted lower case ‘o’ characters used instead of a proper degree symbols, you can search for them and replace them with the correct symbol:

  1. Open the Find and Replace window (Ctrl+h).
  2. In the ‘Find what’ field, type a lower case o.
  3. With your cursor still in the ‘Find what’ field, click More.
  4. Click Format and select Font.
  5. Click the Superscript checkbox until it has a check mark in it.
  6. Click OK to close the Find Font window.
  7. Put your cursor in the ‘Replace with’ field.
  8. Type ^0176
  9. With your cursor still in the ‘Replace with’ field, click Format and select Font.
  10. Click the Superscript checkbox until it is clear. You may have to click it twice.
  11. Check your Find and Replace window looks like the screenshot below. If it does, click Find Next and then Replace for each one found.

Related: Prime and double prime symbols: https://cybertext.wordpress.com/2018/09/23/word-insert-a-prime-and-double-prime-characters/

h1

Word: Find ‘ing’ words and change their formatting

September 21, 2018

Tessa had a problem—she needed to find all words ending in ‘ing’ in her document and format the whole word in some way.

NOTE: This find/replace will find ALL words ending in ‘ing’, but not words ending in ‘ings’ or ‘ingly’. And words such as ‘going’, ‘bring’, ‘sing’, ‘king’, and ‘thing’ also get found. But if your aim is to find ALL words ending in ‘ing’ that’s what you’ll get.

Simplest solution: If you didn’t want the whole word to be formatted, just the ‘ing’ bit, then it’s easiest to use the standard find/replace, with the ‘Match Suffix’ option turned on. See Method 1 below.

However, if you want the whole words found and formatted, you’ll need to use wildcards. See Method 2 below.

Method 1

  1. Open the Find and Replace window (Ctrl+h).
  2. Click More to see the extra search options.
  3. In the ‘Find what’ field, type ing
  4. Select the Match suffix checkbox.
  5. Put your cursor in the ‘Replace with’ field, then click Format at the bottom of the window.
  6. Click Font.
  7. Select the formatting you want to apply to the found ‘ing’s—you can choose one or more options from this window. Then click OK to close the Font window. The formatting you selected is listed below the empty ‘Replace with’ field.
  8. Click Find Next, then click Replace as many times as you need to be comfortable that the find/replace works as you want it to. If you’re happy with the matches, then click Replace All.

Method 2

This method uses Word’s find and replace with wildcards.

  1. Open the Find and Replace window (Ctrl+h).
  2. Click More to see the extra search options.
  3. Select the Use wildcards checkbox.
  4. In the ‘Find what’ field, type <[A-Za-z]@ing>
  5. Put your cursor in the ‘Replace with’ field, then click Format at the bottom of the window.
  6. Click Font.
  7. Select the formatting you want to apply to the found ‘ing’ words—you can choose one or more options from this window. Then click OK to close the Font window. The formatting you selected is listed below the empty ‘Replace with’ field.
  8. Click Find Next, then click Replace as many times as you need to be comfortable that the find/replace works as you want it to. If you’re happy with the matches, then click Replace All.

How this wildcard find/replace works:

  • < and > represent the start (<) and end (>) of a word (this specifies that you’re looking for a whole word)
  • [A-Za-z] look for any upper any lower case letters
  • @ing tells Word to repeat looking for upper/lower case letters until it finds ing

What about ‘ing’ in the middle of a word?

Follow Steps 1 to 3 in Method 2 above, then in the ‘Find what’ field, type <[A-z]@ing[a-z]@>. Continue with Steps 5 to 8 above.

NOTE: You can’t format just a part of the replace (i.e. you can’t make just the ‘ing’ in ‘fringed’ red or bold)—it’s all or nothing.

h1

Word: Make some specific text bold

September 14, 2018

As most people would know, you can apply bold formatting in Word using Ctrl+b or the Bold icon on the Home table on the Ribbon.

But what if you want more? What if you have some specific text scattered throughout your document that you want to make bold in one action? This was the issue Colin faced. In a comment on one of my other blog posts, Colin asked if there was a way to apply bold a set of characters that all started with the same code, but had different numbers after that code (e.g. like product codes). In his example, he had a lot of instances of VA-001, VA-002, etc. with the last three digits being different each time. He’d tried the method I’d documented in my earlier post, but he could only bold part of the code, not all of it.

As with any find and replace, once you identify the pattern, it’s easy enough to test various ways of finding a match and then applying the change to it. I figured out two methods—both using wildcards. The first method assumes there are only ever three characters (not, 1, 2, 4, etc.) after the VA- part, while the second applies to any length of the ‘word’ after VA-.

For both methods, open the Find and Replace window (Ctrl+h), click More, then check the option for Use wildcards.

Method 1: Only three characters

  1. In the Find field, type: (VA-)(???)
  2. In the Replace with field, type: \1\2
  3. With your cursor still in the Replace with field, click Format, then Font, then select Bold. You should see Font: Bold directly below the Replace with field (see screenshot).
  4. Click Replace a few times to make sure the find/replace is doing what you expect it to. Once you are satisfied, you can click Replace All.

Notes:

  • In the Find, the code is separated into two parts, both surrounded by parentheses—the VA- part, which is a constant in what Colin had, and three question marks (???). A single question mark represents any single character, so by typing three question marks, you’re asking Word to look for ANY three characters (letters and/or numbers) after the VA- part. If you only had two characters, then you’d type two question marks; if you had 4, then you’d type four, etc.
  • In the Replace, you’re replacing what was found in both parts with itself. In other words, you’re not changing anything. What you are doing in the Replace, though, is specifying that what you find and replace with itself is now bold (step 3).

Method 2: Any number of characters

  1. In the Find field, type: (VA-)(*>)
  2. In the Replace with field, type: \1\2
  3. With your cursor still in the Replace with field, click Format, then Font, then select Bold. You should see Font: Bold directly below the Replace with field (see screenshot).
  4. Click Replace a few times to make sure the find/replace is doing what you expect it to. Once you are satisfied, you can click Replace All.

Notes:

  • In the Find, the code is separated into two parts, both surrounded by parentheses—the VA- part, which is a constant in what Colin had, and an asterisk followed by a right chevron arrow. The asterisk represents any number of characters from one to infinity. Because a ‘character’ in Word could be a space, you don’t want it to find EVERY character after the VA- part—you’d get the whole document! So you add the > to tell Word to stop at the end of the ‘word’ it finds. In other words, it will stop at any character that typically follows a set of adjacent characters (a ‘word’), such as a space, period, comma, colon, semicolon, etc.
  • In the Replace, you’re replacing what was found in both parts with itself. In other words, you’re not changing anything. What you are doing in the Replace, though, is specifying that what you find and replace with itself is now bold (step 3).

[Links lat checked September 2018]

h1

Word: Macro to convert from one style to another

September 11, 2018

I should have hunted this out years ago. My main client has a habit of changing templates every couple of years, which means the old docs have to go onto the current template when they get revised. It’s a pain and involves quite a few steps (and up to several hours) per document. I still have to do the cover page and headers/footers manually, and all the landscape/portrait sections, but now I’ve got a way to convert styles in the older template to differently named styles in the newer template. For styles with the same name, no such conversion is necessary, but when the old style is called ‘Table Bullet 1’ and the new one is called ‘Table Bullet’ you have to either reapply the correct style everywhere it is used (ugh!), do a global find and replace for the style, or do a ‘select all’ for the style and then click the new style to apply it.

Some caveats:

  • Test first. Test on a COPY of the old document before doing it on the original copy, and any other documents in the set.
  • Make sure you’ve applied the new template to the old document so that the new styles are available in the document. (or, use the Organizer to copy styles across from the old template to the new [longer, slower method]).
  • Make a list of the old styles and the matching, but differently named, new styles—make sure you write down the style names EXACTLY (including any hyphens in the style name) otherwise this macro won’t work.
  • This macro runs a find and replace and replaces ALL. Test first.

Acknowledgements: I modified the macro written by Christina and available from here: https://stackoverflow.com/questions/29953322/changing-styles-in-word-using-vba to suit my client’s situation.

In my modified macro below, several styles used in tables had name changes, as did one for the appendix headings. You can add more for your circumstances—just make sure you add a new ‘Set’ line with a unique name, call that name in a separate ‘With’ statement, and make sure you add that name to the ‘Dim’ statement at the top of the macro.

If you want to use/modify this macro, copy/paste the code below–some of it runs off the page, so if you retype the code you may miss some critical command. Then modify the Set names (if required) and the style names in the double quotes.

Sub FNR_Styles()

' Converts old table and Appendix styles to new using find and replace
' Rhonda Bracey, 11 Sept 2018 (modified from https://stackoverflow.com/questions/29953322/changing-styles-in-word-using-vba [Christina])

Dim objDoc As Document
Dim table1 As Style, table2 As Style, table3 As Style, table4 As Style, app1 As Style

Set objDoc = ActiveDocument
' This code does *NOT* protect against the possibility that these styles don't
' appear in the document. That's probably not a concern with built-in styles,
' but be aware of that if you want to expand upon this for other uses.
' Set up as many of these as you need, giving each a unique name.
' The style name in the quote marks is the NEW style name.
' Make sure you add the name to the second Dim row at the top of the macro as well.

Set table1 = ActiveDocument.Styles("Table Text")
Set table2 = ActiveDocument.Styles("Table Bullet")
Set table3 = ActiveDocument.Styles("Table Heading White")
Set table4 = ActiveDocument.Styles("Table Text Number")
Set app1 = ActiveDocument.Styles("Appendix Heading")

' Searches the entire document (but not foot/endnotes, headers, or footers)
' for the old style and replaces it with the new style.

With objDoc.Content.Find
    .ClearFormatting
    .Style = "Table Text - Left"
    ' The style in the quote marks is the OLD style name
    With .Replacement
    .ClearFormatting
    .Style = table1
    End With
    ' Below is the Replace All command.
    ' Could change this to Replace:=wdReplaceOne to replace one only
   
    .Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With

With objDoc.Content.Find
    .ClearFormatting
    .Style = "Table Bullet 1"
    With .Replacement
    .ClearFormatting
    .Style = table2
    End With
    .Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With
With objDoc.Content.Find
    .ClearFormatting
    .Style = "Table Head - Center"
    With .Replacement
    .ClearFormatting
    .Style = table3
    End With
    .Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With
With objDoc.Content.Find
    .ClearFormatting
    .Style = "Table Text - Number"
    With .Replacement
    .ClearFormatting
    .Style = table4
    End With
    .Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With
With objDoc.Content.Find
    .ClearFormatting
    .Style = "App H1"
    With .Replacement
    .ClearFormatting
    .Style = app1
    End With
    .Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
 End With

End Sub

 

h1

Word: Add a character to all items in a long list, and style it in a colour

July 27, 2018

Here was a tricky one posed by my husband first thing this morning. He had a very long column in a table (some 2750+) rows, in which he had some sort of code, like a product identifier. Fortunately, the codes were all the same — they were all K-xxxx, where ‘xxxx’ was a 4-digit number (e.g. K-1234, K-5432, etc.).

He wanted to add a zero immediately after the hyphen. Easy enough. But he wanted this zero character to be blue! Hmmm… (and no, I have no idea why! Update: He had a list of catalogue numbers, but when the company used up all the 4-digit numbers, they changed to 5-digit numbers. To sort them correctly by catalogue number in Word, he needed to add a leading 0 to the 4-digit ones, but he wanted to show that the 0 wasn’t part of the original number, thus the colour.)

After a few minutes of testing I achieved what he wanted. I had to do three find and replace passes, with one of them a wildcard find/replace — the first pass added the 0, the second changed the colour of the hyphen and its trailling zero to blue, and the third changed the colour of the hyphen back to black, leaving just the 0 after the hyphen in blue text.

NOTE: If you’re doing something like this on your own document, either work on a COPY until you’ve refined the procedure and know you won’t inadvertently replace something you shouldn’t have, OR at the ‘Replace All’ steps below, click ‘Replace’ instead, followed by ‘Find Next’. You will have many more clicks to do, but it’s a safer option.

Here’s what I did:

First pass – add a zero after the hyphen:

  1. Open the Find/Replace window (Ctrl+H).
  2. In the Find What field, type K-
  3. In the Replace With field, type K-0
  4. Select the list (or column in a table) you want to apply this change to
  5. Click Replace All. This adds a zero after the K-, so you end up with codes like K-01234, K-05432, etc.
  6. Leave the Find and Replace window open.

Second pass – make the hyphen and the zero another colour:

  1. For the second pass, click outside the selection to position the cursor away from it (I had to do this because as soon as I entered the wildcard string for the colour, ALL the selected text changed to blue, without me even clicking Replace All — very strange).
  2. In the Find/Replace window, click More.
  3. Select the Use wildcards checkbox.
  4. In the Find What field, type (-)(0) (there are NO spaces in this string).
  5. In the Replace With field, type \1\2 (there are NO spaces in this string).
  6. With your cursor still in the Replace With field, click Format.
  7. Select Font.
  8. Choose a colour from the Font color drop down.
  9. Click OK.
  10. Check the Replace With field — it should have Font color: <name of colour> below the field. The only thing below the Find What field should be Use wildcards. If you have something different, repeat these steps, and make sure you follow Step 6 exactly.
  11. Select the list (or column) again.
  12. Click Replace All. This changes the hyphen and trailling zero to the colour you selected.
  13. Leave the Find and Replace window open.

Third pass – remove the colour from the hyphen:

  1. For the third and final find and replace pass, click outside the selection to position the cursor away from it. Don’t forget to do this!
  2. Clear the Use wildcards checkbox.
  3. In the Find What field, delete the existing characters, then type a hyphen.
  4. In the Replace With field, delete the existing characters, then type a hyphen.
  5. With your cursor still in the Replace With field, click Format, select Font, then in the Font Color drop-down box, select Automatic (or another font colour).
  6. Select the list (or column) again.
  7. Click Replace All. This changes the hyphen colour to the colour you selected in Step 5.

 

h1

Word: Wildcard find and replace for numbers inside parentheses

July 22, 2018

In a comment on another post (https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-punctuation/), AVi asked if there was a way to find percentage numbers (e.g. 56%) that were inside parentheses, and replace them with the same number but without the parentheses — i.e. (56%) becomes 56%.

There is, but it’s a bit trickier than usual because parentheses are also special characters in Word’s find/replace lexicon—these have to be ‘escaped’ for Word to treat them as normal characters and not as special characters.

In figuring this out, I also took into account that there might be single numerals (e.g 4%), triple numerals (e.g. 125%), and numerals with one or more decimals (e.g. 75.997%).

Here’s what I came up with that worked for all those scenarios:

  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: ([\(])([0-9]*%)([\)])
  4. In Replace With, type: \2
  5. Click Find Next, then click Replace once the first is found. Once you’re happy that it works, repeat until you’ve replaced them all.

What the find and replace ‘codes’ mean:

The three elements (each is enclosed in parentheses) of the Find are:

  1. ([\(]) — 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 surround this string in square brackets [ ] (otherwise, it won’t work).
  2. ([0-9]*%) — The [0-9] represents any number from 0 to 9; the * represents any more characters immediately after that number (more numbers, or a decimal point), thus not limiting the find to only single digit numbers; and the % symbol says this string of numbers found must finish with that symbol. This finds numbers like 2%, 25%, 283%, 25.4%, etc.
  3. ([\)]) — You need to find the closing parenthesis, so you need to enclose it in parentheses. However, because the closing parenthesis is a special wildcard character in its own right, you need to tell Word to treat it as a normal text character and not as a special character, so you ‘escape’ it with a backslash ‘\‘ before the ), then surround that string in square brackets.

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 .

For the Replace: \2 — Tells Word to replace the second element of the Find with what was in the Find (i.e. a number followed by a % symbol) .

AVi: 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).

 

h1

Word: Finding duplicate words

July 11, 2018

I had a long list (57 pages!) of Latin species names, sorted into alphabetical order. I’d separated the words so that there was only one word on each line. My next task was to go through and remove all the duplicates (i.e. a word immediately followed by the same word) so I could add the final list to my custom dictionary for species in Microsoft Word. I started doing it manually—it’s easy enough to find duplicates when the words are familiar, but for Latin words, my brain just wasn’t coping well and I was missing subtle differences like a single or double ‘i’ at the end of a word. There had to be a better way…

And there is! Good old Dr Google came to the rescue, and with a bit of fiddling to suit my circumstances (one word on each line), I got a wildcard find and replace routine to find the duplicates.

NOTE: DO NOT do a ‘replace all’ with this, in case Word makes unwanted changes. In my case it didn’t treat the second word as a whole word for matching purposes (e.g. it thought banksi, banksia, and banksii were duplicates). Even though I had to skip some of these, it was still worth it to automate much of the process. Another caveat—if you have several lines of the same word, each pair will be found, but you’ll have to run the find several times to get them all. Much better to move your cursor into Word and delete the excess multiple duplicates when you find them. You may still have to do a couple of passes over the document, but the heavy lifting will have been done for you.

Here’s what I did to get it work:

  1. Press Ctrl+H to open the Find and Replace window.
  2. Click More, then select the Use Wildcards checkbox.
  3. In the Find What field, type (<*>)^013\1 (there are no spaces in this string).
  4. In the Replace With field, type \1 (there are no spaces in this string either).
  5. Click Find Next.
  6. When a pair of matching whole words is found, click Replace. NOTE: If the second word is only a partial match for the first word, click Find Next.
  7. Repeat steps 5 and 6 until you’re satisfied you’ve found them all.

How this works:

  • (<*>) is the first element (later represented by \1) of the find. The angle brackets specify the start and end of a word, and the ‘word’ is anything (represented by the *). In other words, you’re looking for a whole ‘word’ of any length and made up of any characters (including numbers).
  • ^013 is the paragraph marker at the end of the line. In my situation, each word was on its own line with a paragraph mark at the end of the line. If you don’t have this situation, leave this out and replace it with a space (two repeated words in the same line are separated by a space). NOTE: Normally you can find a paragraph mark in a Find with ^p, but not with a wildcard Find—you have to use ^013.
  • \1 is the first element. In the Find, it means the duplicate of whatever was found by (<*>); in the Replace, it means replace the duplicated word with the first word found.