Simplify Manuscript Preparation with Don's Super Macro
by Don Mankowski

Download Word files WTEdit and Sample

Recently, Sarah (my favorite author-editor) explained the editing steps that she had to do with most documents that she prepares for publication.As you might expect, it occurred to me that we ought to be able to automate much of the process.

The resultant program was developed in Microsoft Word 2000ís Visual Basic for Applications (VBA) and was tested successfully in Word 97.I suspect that it would not work as-is in Word 6.0 and earlier, unless reverse-engineered to WordBasic.I believe that Word 95/Word 7 is in the same incompatible class, but I could be wrong.(Try it and let us know.)

Defining the Task

For a given block of text (up to and including an entire document), we want to:

The whole idea is to trim the manuscript to something that will format cleanly and consistently in the new environment.We get rid of faked indentation via spaces and tabs, and make other things consistent.My task was to write six macros and tie them together.

You can incorporate and use these functions without understanding anything of how they were put together.Just skip ahead to Installing the Functions.

Setting Up the Parts

The word file WTEdit.doc has the source code for seven macros, the six sub-tasks and one that ties them all together.You may want to load WTEdit.doc for reference.

Getting rid of the extra spaces is a good first step, for being able to count upon single spacing simplifies the remaining steps.In an earlier discussion on MS Word macros [More WORD Tricks],I presented a macro named OneStepMax and discussed its construction.† That one leads off our lineup.

Although Iíve worked quite a bit with Visual Basic, I have no good reference on the routines and the variables that comprise MS Word, so my method was to use on-the-spot keyboard macro recording (as described in my earlier article), and then editing of the resultant code after the fact.I inserted comments (the lines beginning with a single quote are comments rather than executable code) to tell you what specific segments are doing, in case youíre interested.

Next up was a SmartenQuotes macro.The code here turns on Wordís smart-quote function, then executes a search-and-replace of all double quotes, followed by a search-and-replace of all single quotes (including apostrophes).As a concession to the user, I recorded the status of the smart-quote flag in use at the time, and restored it to whatever it had been after completion of my task.This is a good idea if you donít want your users to get sore at you.

For the macro NoLeadSpaces, the logic simply executes a search-and-replace for any space immediately after a paragraph mark.If you think about it, this serves to clean up the field immediately before the next paragraph.(Well, it wonít help the very first paragraph, and I could have added another step to do this, but didnít do that just yet.)We donít have to worry about two, three, et cetera spaces after a paragraph mark, because we reduced those to a single space earlier.

The logic for NoLeadTabs is quite similar: reduce any paragraph-mark-plus-tab pair to just the paragraph mark.I went an extra step and did the same for any paragraph mark followed by two tabs, but stopped there Ė three or more, youíre on your own.I suppose that we could have compressed multiple tabs beforehand, as we did with spaces, but that may have wreaked havoc on some legitimate tabs used in tables.Thereís such a thing as going too far.

Macro EmDashFix does several search-and-replace operations.We replace any double dash with the single character em-dash.Then we clean up any space either before or after the new em-dash character.Finally, any em-dash followed by a hyphen is cleaned up as well, for cases when the original writer used a triple hyphen.

In AddEplipses, we replace three spaced periods by the ellipsis character.Then we do the same for three unspaced periods.Next, we clean up any spaces before or after our new ellipsis characters.

I added a working version of the StripParaMark macro (the one that removes excess paragraph marks from a file) that we constructed in an earlier lesson.Itís not part of this particular operation, but now it will be there if you need it.

Putting It Together

Now, any of these macros can be called individually to accomplish its function (although all the others depend rather strongly upon a prior execution of OneSpaceMax to function best).We want to simplify the process, so we create macro WTEdit (i.e., WordThunder Edit), that simply calls each of the others in turn.

Thereís an advantage here: if you want to omit one of these cleanup steps, you need only delete that particular call from WTEdit (or else turn that line into a comment.).Or, if youíre inspired to create additional macros of your own, you can add a call to the new function here.

In testing, we found that on a large document it could take a few seconds to do its various jobs, so I was inspired to call up a pop-up message box to inform the user that it was finished.I also added an error message if the user called it without having selected any test.The functions were set up to work on user-selected text, rather than selecting text itself or assuming that the user wanted to correct an entire document.This allows you to clean up parts of a long document, or the who thing via a simple ďselect allĒ prior to execution.

Installing the Functions

1.Start MS Word.

2.Load the file WTEdit.doc as any other Word file.This is the source code for the new functions.

3.Do a Select All (Edit | Select All) and Edit | Copy to put the source code in the buffer.

4.Next, we go into macro edit mode.Tools | Macro | Macros and on the new screen.There are two possibilities here.

4A.If the [Edit] button is greyed and inaccessible, then you donít have any user-defined macros as of yet.Type WTEdit in the Macro Name box and then click [Create].Clear out any text in the new window that comes up, as weíll be providing the whole thing.Then, go on to step 5.

4B.If you see an [Edit] button, click it.This brings, in an edit window, the source code for the existing macros to which you have access.Press [Ctrl]+[End] to go to the end of this file.Itís important that you have the cursor after the last line that reads ďEnd SubĒ.

5.Edit | Paste to add the new macros to this file.(If this doesnít work, you may have forgotten to Copy the text, or somehow cleared the buffer.If this is the case, youíll have to go back to the window in Word proper and Copy it again.)

6.File | Save Normal to save the changes.(Or, [Ctrl] + [S], or click on the diskette icon.)

7. File | Close and Return to Microsoft Word, (or [Ctrl] + [Q]).

8.Now, youíre back in Word proper.You probably want to do File | Close to get rid of the macro source, as you donít need it any longer, and probably want to keep your copy intact in case you need it later on.

9. Test it.Load a file that you want to convert.Weíve provided Sample.doc for you.

10.Select some text as you usually do, or the entire file.

11.Activate the Macro via Tools | Macro | Macros.Find and highlight WTEdit in the list box; you may have to scroll it down.Then select the [Run] button.(Or, you can simply double-click on WTEdit.)

12.If you forgot to select any text, the program will warn you.Click [OK], select some text and try again.Otherwise, the program does its thing and tells you when itís finished.Click [OK] and check the results.

13.If you like what youíve seen, you can assign this macro (or any of its sub-functions) to a key (e.g., [F8]) a key sequence (e.g., [Ctrl]+[W], [Ctrl]+[Alt]+[E]), or put it on one of your menus by following instructions that I provided in [Taking Control of your Installation], an earlier article.

Letís hear your input.Perhaps we can add more functionality to the utility.

Also see Creating Macros in MS Word, More WORD Tricks and Taking Control of your Installation

© 2002 by Don Mankowski