
TO ASK A QUESTION: If you have a question or need help with Office, please feel free to use the 'Click to Contact' link at the bottom of this page. You'll get a form that you can use to email a question to me. (I had been getting a ton of spam when accepting direct emails, so only emails that use this form will get through to me.)
Please be sure to mention the version of Office you are using when you send your question.
I answer all e-mails that I receive via this form, as long as they are polite :)
Since disabling comments on this site, I'm actually hearing from more of you with questions ... so, as it seems people prefer to email rather than comment, I'm going to leave comments disabled. As always, you can ask me any Office-related questions you have. If the question is outside of my expertise, I'll try to direct you to where you can get an answer.
Get Rid of Excess Paragraph Marks (Word)
A nice person just posted a comment to an earlier article and mentioned in there wanting to write a macro in VBA to get rid of excess paragraph marks in documents. Good idea.
So, I thought I'd post a tip on that -- both to give you the quick way of doing this without code and to give you the code if you want a macro for it as well, to save even a bit more time (for those of you who need to take this step often). Here goes:
Word enables you to find and replace special characters as well as formatting, in addition to just text. So, you can replace any instance of 2 paragraph marks in a row with 1 paragraph mark that has space after the paragraph (or that has a particular paragraph style attached (containing your paragraph spacing), if you prefer). To do this:
1 - Press CTRL + H to open the Replace dialog box and then click More to expand the dialog box.
2 - If any formatting is listed beneath the Find or Replace text boxes, click into the text box and then click No Formatting.
3 - Click into the Find box and type ^p^p
Note: ^p is the symbol for paragraph mark in the Find and Replace dialog boxes. If you would rather select it than type it - click the Special button and select it from the pop-up menu. Important: do not select Paragraph Character from that list, as that won't find the same thing.
4 - Click into the Replace box and type ^p then click the Format button, select Paragraph from the pop-up menu and add your preferred space before or after the paragraph. When you click OK from the Paragraph dialog box, confirm that the spacing you applied is listed under the Replace box and not the Find box (this happens to me all the time :)
5 - Click Replace All to replace throughout the entire document. Note that, if there are instances of three or four paragraph marks in a row, you might have to click Replace All a few times to get rid of all excess paragraph marks. You can just keep clicking it until the message box at the end of the task tells you that zero replacements were made.
Okay ... now, how does this translate to a macro? Read on for quick details ...
If you record the steps while you're doing the task just described, you will (as usual with a recorded macro) get a bit more code than you need, but frankly -- it's not a lot of extra code in this case, and you can just add the recorded macro to a toolbar button or keyboard shortcut and be done with it :) But ... you can edit it a bit if you want to -- mostly, in this case, just to understand the macro if you're curious. But also to make sure it recorded the steps you'll need to make sure that it works correctly regardless of where you use it.
Here's the code I get when I record the steps described earlier - choosing 12 pts after the paragraph as my spacing:
Sub Macro9()
'
' Macro9 Macro
' Macro recorded 6/30/2005 by Stephanie Krieger
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineUnitAfter = 0
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Now, many people who develop with VBA don't like the Selection object, and there's good reason for that when you're writing a lot of code that needs to work efficiently. But, for a little productivity macro like this -- it isn't going to hurt anybody! This is also the best way to go because then you have the choice to search a selection or the whole document (when you use the Selection object for Find and Replace with nothing selected, it will search the entire active document story (that is, the main document if that's active or all headers and footers if one is active, etc.).
Also -- you don't want to delete some of these lines of code because they specify settings in the Replace dialog box. Remember that Find and Replace keeps its last setting within a Word session -- so, for example, if you removed the lines of code for clearing formatting and you had just executed a Find or Replace for formatting, that formatting would be retained in your new search, and you might not get the replacement result you want. So - here's what I would end up with after editing this macro:
(By the way - for best practices and easy-to-read code, you really should indent code within loops and groups, etc... I always do -- but when pasted into Moveable Type for presentation here, they're removed. It's a pain to replicate that visually on the blog, so I didn't take the time. But, do let me know if the lack of indents is deeply disturbing ;-)
Sub ReplaceParMarks()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.ParagraphFormat.SpaceAfter = 12
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Notice that I deleted the extra paragraph spacing-related lines of code that were added by the recorded macro. That's because I don't want to affect anything else about the paragraph formatting I'm replacing besides the space after. So, if a paragraph has space before -- leave it be. Note also that, if you have places where three or four paragraph marks need to be replaced - you might want to run a replace for 4 paragraph marks with one that has 36 points after, then for 3 marks with one that has 24 points after ... etc :)
Meanwhile -- if these macros or what I've said about them were totally greek to you, check out a recorded version of my recent webcast on the basics of VBA for document production (find the link in the right-hand column of this page). Or, for a brief intro to editing your recorded macros (along with some other power user tips) register for my upcoming live webcast on July 19th - Advanced Word Document Tips and Tricks. (link is in the preceding post directly below this one).
And, do post comments if you need more explanation or have anything to add on this topic :)
... by the way ... I still owe two tip posts that I promised a couple of weeks ago. Other questions keep popping up that push those back. If you've been waiting for one of them, post a comment and let me know. Otherwise, I'll get to those as soon as I can!
Happy Thursday!
TrackBack URL for this entry:
http://www.arouet.net/cgi-bin/mt2/mt-tb.cgi/101
Comments
Hi! I've recently downloaded Office 2007 beta 2 but, unfortunately I can't do much with it. I work extensively with wordprocessing in Greek so it seems that I need to include a Greek dictionary to the Word 2007. Any idea how to do it? Thanks, John.
Posted by: John K. | September 15, 2006 06:24 AM