Written by Allen Wyatt (last updated November 30, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
Lisa needs a way to find the last two words in all the paragraphs of a document and replace the space between those words with a non-breaking space. She wonders if this is possible using Find and Replace.
The easiest way to do this without using a macro is to rely on the wildcard capabilities of Find and Replace. The key to doing this is to remember that you don't really need to identify the last two words in what you find—you only need to identify the last word, with its preceding space. Follow these steps:
Figure 1. The Replace tab of the Find and Replace dialog box.
How this actually works may take just a bit of explaining. The trick here is what you are searching for, specified in step 4. Each element being searched for is surrounded by (parentheses), so it is easy to pick them out and to refer to them in the Replace With box (step 5). The four elements in the Find What box (again, surrounded by parentheses) are as follow:
The first element is a single space. The second is any number of digits, letters, or dashes. (It is the last part of this element—{1,}—that specifies there can be any number of digits, letters, or dashes.) This is the element that specifies the word at the end of the paragraph, and it includes the possibility of the word containing dashes so that compound words are treated as a single word. The third element is any of the punctuation marks shown, and the fourth element is the actual end-of-paragraph marker.
The Replace With specification (step 5) simply replaces the first element (the space) with a non-breaking space, and then puts the second, third, and fourth elements back into place.
It should be noted that there are a couple of scenarios in which this Find and Replace approach will not work. First, it won't work if your paragraph has trailing spaces, between the punctuation mark and the end-of-paragraph marker. You can use Find and Replace to get rid of these throughout your document before doing the above Find and Replace.
The second situation where it won't work is if your paragraph ends with compound punctuation, such as a period followed by a quote mark or a quote mark followed by a colon. The wildcard search expects there to be only a single punctuation mark at the end of the paragraph, and it must be one of the four punctuation marks specified.
The third situation where it won't work is if there is some sort of punctuation in the final word of the paragraph. For instance, if the final word is actually a number such as 1,234 then the inclusion of the comma will stop the match from occurring. This could be dealt with by including the comma in the second element being searched for, but the number of times this might happen seemed miniscule enough that I didn't allow for it in the steps above.
If you prefer a macro-based approach to the substitution, you might consider using the following macro:
Sub LastTwo() Dim p As Paragraph Dim J As Integer Dim K As Integer Dim sTemp As String For Each p In ActiveDocument.Paragraphs J = p.Range.Words.Count For K = J To 1 Step -1 sTemp = p.Range.Words(K) If Right(sTemp, 1) = " " Then p.Range.Words(K) = RTrim(sTemp) & Chr(160) K = 1 End If Next K Next p End Sub
The macro steps through each paragraph of your document and then steps backwards through the words in the paragraphs. It looks for the first word that contains a trailing space, and then replaces that space with a non-breaking space.
There is one interesting thing about this macro—it may not work as expected on all systems. If, after running it, you don't get a non-breaking space but instead see a small "cross" between the last two words, then simply replace Chr(160) in the code with Chr(202). It would be much easier if Word included a defined enumeration for a non-breaking space, but it doesn't. Instead, you need to rely on the ASCII code for a non-breaking space, which doesn't appear to be the same on all systems.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13577) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
You may have boilerplate text that you need to include in your document, and it would be detrimental to accidently change ...
Discover MoreWant to repeat the same Find and Replace operation over and over again? Here are a couple of ways you can improve your ...
Discover MoreHTML tags are great when you want to display information on a web page. They are not so great when you have them in a ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-12-02 12:15:49
Andrew
How about, more simply, a wildcard replacement of " ([! ]@^13)" (that's a single space at the start and one following the explanation point) with "^s\1"? This essentially translates the original problem into "replace the last space of a paragraph with a non-breaking space.
This still won't pick up a paragraph that ends with one or more spaces -- I fix this with the NON-wildcard global replacement of "^w^p" with "^p".
Andy.
2024-12-02 04:07:06
Stephen Barker
Thanks for the links @Tomek and @Beepee
2024-12-02 00:42:11
Tomek
@Beepee and Stephen:
Another useful reference is:
https://wordmvp.com/FAQs/General/UsingWildcards.htm
While the book is more in-depth (as far as I can tell from reading only parts of it), the web page is more concise and may be easier to use if you just want a quick reminder of a particular syntax details.
2024-11-30 10:41:02
Beepee
Stephen.
If you want to get a good understanding of Wildcards in Word try this site:
https://intelligentediting.com/blog/free-e-book-wildcard-cookbook-for-word/
Leads you to a download of:
Wildcard Cookbook for Microsoft Word by Jack Lyon
(I hope AW doesn't mind this reference - Jack Lyon makes reference to Word.Tips!)
2024-11-30 06:38:15
Stephen Barker
Thank you for this clear explanation of how to use Word's Find/Replace facilities. The help (F1) available from Word itself seems to be becoming less and less useful. I find the lack of documentation when I try to use wildcards in Find most frustrating, and your example here (especially element 2) shows how to cover many of the scenarios I have, without using wildcards in the Find.
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments