We are building several functions to streamline our previous move sentence macro. This article is a work in progress as the various functions are developed in upcoming articles.
Thanks for your interest
This content is part of a paid plan.
Problems with Original Long Macros
The extended content below shows the original macros that move a sentence backward or forward in a document.
Given their complexity compared to other editing macros we’ve done in Word (and we haven’t even added handling dialog naturally which I consider a minimum for an author), they serve as school of hard-knocks examples on why functions and subroutines were created.
If you’ve been following along with at least some of the articles, I generally explain many or even most details. Talking about functions implies an intermediate level student, so I’ll skip some of the extended explanations as we proceed through the associated articles.
The Problems
In the original article, the base macro to accomplish the raw task of moving a sentence is not too bad, but the accumulated steps to handle the special cases (so they work more intuitively) make the macros awkwardly long. However, upon inspection, there are several natural sets of steps that can be neatly separated out and would probably apply to other macros.
Candidate functions for move sentence macro
Encapsulating repetitive tasks is a major use of functions and subroutines. After checking the long versions of the macros, some natural functions pop out.
How to decide on a function?
We’re looking for chunks of steps that perform a specific task or a related set of tasks—not too big or it partially defeats the point of creating the functions.
Sometimes the candidate steps can be obscured if they are interspersed among other more macro specific steps, but usually it’s relatively obvious which steps to extract.
Those best suited to functions would ideally be general purpose and probably usable in other macros. On the other hand, some long macros could still be broken up into unique functions or subroutines to make the work more manageable.
Natural functions
Several examples of general purpose sets of steps in the above macros include:
- Check and delete any spaces at the end of a paragraph (used four times)
- Check and add any spaces before or after the moved sentence as needed (used about four times depending on how it’s counted)
- Check and delete an empty paragraph (used twice)
The first two take more steps to implement, but the third is still worth creating a function since it is a task we would likely reuse in other macros.
Secondary tasks
Sometimes we can also group basic tasks together even if they don’t contain a lot of steps.
- Select the sentence (used twice)
Moving the sentence selection to a function will further allow us to more easily modify it later to naturally handle dialog or parentheses.
Modular macros
When we’re done, the move sentence macros should include only the necessary steps to implement the specific tasks. Most, if not all, generic subtasks will be relegated to dedicated functions or subroutines.
All together, these functions will also make the original macros easier to read assuming we pick function names that describe their actions well.
In addition, we’ll have a growing toolbox of useful functions and subroutines already tested and ready to help us create better macros in the future. Thinking at a higher level (more task than step oriented) makes creating macros easier and usually faster. You don’t always have to descend down to the nitty-gritty, itty-bitty steps to get something done.
Imperceptible efficiency hit
Using functions results in a slightly less efficient macros since some additional steps and validation checks are typically performed internally in the subroutines, but you will not notice a practical time difference when running the macro. Unless you’re creating a macro that processes your entire novel every time, or something to that effect, the time savings using functions in your macros is well worth the imperceptible degradation in running speed.
Relevant Functions
To facilitate the improved macro below, several functions and have been implemented:
- Select the current sentence excluding the paragraph mark. Also, naturally account for dialog and parentheses.
- Isolate the steps to delete any spaces at the end of a given paragraph.
- Pad sentence spacing while accounting for its position in the paragraph.
- Check and delete an empty paragraph.
A couple of supplementary functions that would make things more convenient include:
- Get next character after an arbitrary range (the reason for this one isn't as obvious, but we’ll find it useful later)
- Force delete step for a range (spaces in our case) without allowing Word to automatically reinsert a space
Revised macros using subroutines
Putting everything together, we finally get a more refined version of the move sentence macros. These look a lot more manageable compared to the original longer versions.
Declare ranges
Since we're using the ranges as function arguments (data passed to the functions), we need to declare them explicitly.
Otherwise, we'll get an error.
We didn't need to do this previously because VBA will infer the type of the variable (using a generic type), but VBA is pickier when we send data to functions.
Move sentence backward
Let’s merge this new subroutine into our macro. In order to work, this macro requires the included functions to also be in your VBA editor, so this macro is not yet fully functional (get it …).
Move sentence forward
And for the macro to move the current sentence to the right in the document we have:
VIP technical support
Sometimes debugging more complex macros can feel like playing a Whack-a-Mole game especially as you add new and improved features. Premium members get access to special VIP links for limited technical support as specified in the member benefits [please note support time does not accumulate since I cannot overcommit future scheduling requirements (but I try to be reasonable about it) nor cash out in any way if membership ends or is downgraded; support does not consist of writing entire macros or functions which would instead qualify as a paid service].
Original (Long) Macros
To illustrate the motivation for creating the extra functions, here are the original, awkwardly long, move sentence macros.
Moving sentence backward
Our starting macro to move a sentence backward in the document is:
Moving sentence forward
The move sentence forward version of the macro is also long. We need to adjust the corresponding steps (performed off screen) for moving forward, so the details are a little different.