Home | Tips | Library | Other Authors | Other WP Sites | Writer's Links | Contact | Site Map | Donate

Barry MacDonnell's
Toolbox for WordPerfect

Macros, tips, and templates for Corel® WordPerfect® for Windows®
© Copyright 1996-2017 by Barry MacDonnell. All Rights Reserved.

Page updated Mar 6, 2017

DeSpacer - Reduces multiple adjacent spaces to single spaces. It can optionally relocate/remove stray single spaces.

Download DESPACER.ZIP (v1.07; 12/06/10; 36,919 bytes; contains 2 macros and a WPD document with various short phrases and sentences that have extra spaces in them which you can use to see the effects of playing the macros.)

Compatible with WordPerfect 8 and later versions

WordPerfect 11 users: See important information about using macros in the first release of WP11 (11.0.0.233) at the top of this page.

Downloading, Documentation, Modifications, and Support

Disclaimer, Distribution, and Privacy Policies

•  Alternative - If you are not concerned with relocating some single spaces or possibly removing some adjacent format or bookmark codes (see DeSpacer's description in the next column), see the simpler macro below in Footnote 1.

•  Similar "clean up" macro - DelExtraHR.wcm deletes all multiple adjacent hard return codes ([HRt]) and replaces them with a single hard return. Extra hard returns can be caused by pasting material, overuse of the <Enter> key to separate text, etc. The macro is included as part of a small suite of macros that remove unwanted format codes. See DELCODES in the Library.

•  To find multiple adjacent periods (full stops), commas, etc. use Grammtik (on the Tools menu). It will flag these items -- as well as duplicate adjacent words -- which are often left behind after lots of cut/copy and paste operations. Once flagged you can delete the extra ones directly from the Grammatik dialog.

Contains two macros: DeSpacer.wcm and Relocator.wcm (which is a standalone macro that contains a subset of the functions in DeSpacer, as explained below).

DeSpacer.wcm (v1.07)

Purpose

This macro changes existing text like this:
"Once     upon a time,     "

to normally spaced text like this:  
"Once upon a time,"

Background

Why use this macro?

In some documents, adjacent (i.e., contiguous) spaces may have format codes embedded in them, which can present a problem for traditional Find and Replace procedures (e.g., using Edit, Find and Replace to find two spaces and replace them with one space). Using Find and Replace to replace multiple spaces with a single space will remove some (but, curiously, not all) embedded codes, which is something that you probably do not want.

Also, after you delete text or cut-and-paste text to other parts of a document, you could end up with something like this in your document (where <s> indicates a space character):

The<s>quick<s>brown<s>[Bookmark]<s>fox....

Unlike some other macros or Find and Replace methods, this macro won't delete any codes found between space characters. Using this macro, the above line would be processed and look like this in the Reveal Codes window:

The<s>quick<s>brown<s>[Bookmark]fox....

Additionally, any misplaced spaces adjacent to WordPerfect format codes can be relocated (first menu option). For example -

The<s>[Und On]quick<s>[Und Off]brown<s>fox....
(The quick brown fox....)

will be changed to this -

The<s>[Und On]quick[Und Off]<s>brown<s>fox....
(The quick brown fox....)

Note: The exceptions to relocating spaces in this way are spaces adjacent to Font (type or size) codes, where users may want spaces in specific locations when a new font or size is applied. Therefore, spaces adjacent to [Font] and [Font Size] codes are skipped during macro play.

The Despacer macro can also (optionally) delete unwanted single spaces adjacent to certain codes (e.g., [HRt], [HPg], [HCol], [Cell], [Table Off], [Col Def: Off], [Left Tab], etc.) to help "clean up" the document. There may be some (rare) situations where the macro will not remove all such solitary spaces; therefore, a visual inspection may be required.

You can set these options off to leave such spaces alone, and also turn the menu off to speed operation. See the top of the macro's code for instructions.

Instructions

[1] Download the DESPACER.ZIP archive file (click the link at top of the left column on this page) to any convenient folder or to your Windows desktop. DESPACER.ZIP is merely a "container" that holds the actual macros.

[2] Extract the macros from that ZIP file and place them in your WordPerfect default (or supplemental) macro folder so the program can find them. [If you need help with this see the instructions in the "Downloading..." link.]

[3] Open the document that you want to "despace." Play the Despacer.wcm macro with Tools, Macro, Play (or Alt+F10). You can choose various options on the menu that pops up. [Screen shot] Click OK. A "Working ... Please wait" message will appear and you can cancel operation from it as well as from the main menu.

Tips

☼  Recommended: If you choose to relocate extra spaces adjacent to format codes -- the first option on the pop up menu -- it is recommended that you first clean up "empty" code pairs (e.g., [BoldOn][BoldOff]) with the DelPairs macro in the Library. This should make DeSpacer more accurate with respect to the first option.

☼  You can play the macro on a copy of your document (File, Open, <name>, Open as Copy), then do a comparison (File, Document, Compare) of the copy with the original document to see what has been changed. (File, Document, Compare is not available in the some editions.)

☼  You can assign the macro to a toolbar, keyboard shortcut, or menu for easy access. See here.

Notes

¤  If your document contains hard spaces (i.e., [HSpace] codes, entered from your keyboard with Ctrl+spacebar), the macro will not treat them as spaces and thus will not remove them. You can use Reveal Codes to determine if any remaining (and unexpected) extra spaces are hard spaces and not normal (spacebar) space characters.

¤  If the macro processes a lot of spaces, you may not be able to undo the change with Edit, Undo (which is limited to 300 levels of undo). Normally, the macro will process the document with just one or a few levels of Undo -- but there may be circumstances where this is not true. Therefore you should either abandon the changes and Close without saving (then re-open the document), or use File, Save As, and save it with a different name.

¤  Processing a large document can take time, given the way the macro operates. You may want to play it on a copy of the final draft of your document, after all other edits are done.

¤  Extraneous spaces can be almost anywhere in a document. While the author has tested this macro "nine ways from Sunday," there may be cases where spaces are not removed, or are relocated improperly. If so, please contact the author and he will try to solve the problem and make the macro more robust. In light of this, however, it is recommended that you always work on a copy or a backup of your document.(See disclaimer.)

Relocator.wcm (v1.0)

Note: This macro is based on the first option in the DeSpacer.wcm macro above (screen shot of DeSpacer menu), which can relocate solitary spaces that might be misplaced adjacent to many paired format codes. If you use DeSpacer with that option enabled, you don't need to use Relocator on the same document (unless you want to). [You can also download just the Relocator macro here.]

Purpose

Relocates any single (i.e. solitary) spaces found inside (i.e., between) various paired text attribute codes ([bold>..text..<bold], etc.) to positions immediately outside those codes.

Mis-located spaces can happen if the user has disabled the option "Use WordPerfect 9 text selection" (the Corel shipping default) in Tools, Settings, Environment (found in WordPerfect 10 and later versions), or if the user has received a converted-from-Microsoft-Word document.

For example, these single spaces (indicated by <s>) will be relocated:

From -

[Main screen:]     The quick brown fox....
[Reveal Codes:]   The
<s>[Und On]quick<s>[Und Off]brown<s>fox....

To -

[Main screen:]     The quick brown fox...)
[Reveal Codes:]   The
<s>[Und On]quick[Und Off]<s>brown<s>fox....

Notes

¤  It is recommended that you first clean up "empty" code pairs (e.g., [BoldOn][BoldOff]) with the DelPairs macro in the Library. This should make Relocator more accurate.

¤  The exceptions to relocating spaces in the above manner are spaces adjacent to Font (type or size) codes, where users may want spaces in specific locations when a new font or size is applied. Therefore, spaces adjacent to [Font] and [Font Size] codes are skipped during macro play.


¤  Only single spaces are relocated since it must be assumed that some users actually desire multiple spaces in certain locations. If you want to remove all extra spaces use the author's DeSpacer macro (see above).

Instructions

Simply play the macro in any document. An initial message allows you to proceed or cancel. When it has finished searching the document it will display a message informing you of the number of spaces it relocated. (A status bar message shows the running count, which might be useful with long documents. For short documents which are processed quickly, you might not have time to see it.)

Tip: WordPerfect's File, Document, Compare can be used to examine "before" and "after" versions of the document. (File, Document, Compare is not available in some editions.)

[Page Top]



Footnote 1

If you are not concerned with relocating some single spaces
or possibly removing some adjacent format or bookmark codes (see the DeSpacer description above), here's a much simpler macro that should do the job that DeSpacer does.

It can also optionally retain all double spaces (see the Note below) as well as single spaces, but it does not have the options included in DeSpacer (see the DeSpacer menu here).

To copy the macro below into your WordPerfect program, see the simple instructions here.

Note: If you want to retain all double spaces in the current (working) document, you need to add an extra space character to the SearchString() command below (the default is 2 spaces, so change it to 3 spaces with your spacebar).


// Macro code begins

pResetFindReplace ()  // (call a procedure to set search parameters to the default)
PosDocVeryTop  // (start at the top of the document before all other codes and text)
OnNotFound(EndNow@)  // (when NOT found, jump down and exit)
MatchPositionBefore  // (when found, place the cursor to the left of the item)
SearchString("  ")    // <= 2 space characters between the quotes

While(True)  // (creates an endless loop until a Not Found condition exists)
    SearchNext(Extended!)  // (searches in headers, footers, graphic boxes, etc.)
    DeleteCharNext
    pExitSubstructures ()  // (call another procedure)
Endwhile

Label(EndNow@)
pResetFindReplace ()
// Macro exits here:
Return

Procedure pResetFindReplace ()
SearchString("")  ReplaceString("")
// Only one Match* command can be active at any one time:
// MatchPositionAfter()
// MatchExtendSelection()
// MatchPositionBefore()
MatchSelection()
SearchFindWholeWordsOnly(No!)  MatchWithAttributes(No!)
ReplaceWithAttributes(No!)  SearchCaseSensitive(No!)
ReplaceWithCase(No!)  MatchWithFont(No!)
ReplaceWithFont(No!)  MatchWithFontSize(No!)
ReplaceWithFontSize(No!)  SearchInSelection(No!)
SearchWordForms(No!)  SearchWrap(No!)  MatchLimit(No!)
EndProcedure

Procedure pExitSubstructures ()
// Exit from any header, footer, footnote, endnote, text box, graphic caption, etc.:
While(?Substructure)
    vSubDoc:=?CurrentSubDoc
    SubstructureExit
    If((vSubDoc=10) or (vSubDoc=11)) BoxEnd(Save!) EndIf
EndWhile
vBoxType:=?BoxContentType
// If the cursor is in an image or a text box is selected, exit from it:
If(vBoxType=1 or vBoxType=3) BoxEnd(Save!) Endif
EndProc

// Macro code ends