Create a new paragraph with the current sentence?
Sounds almost pointless at first … until you get used to using it. So read on.
Thanks for your interest
This content is part of a paid plan.
Create the empty macro
A previous post covers creating an empty macro like the one shown below. When you’re done, you’ll have something like:
The single quote tells VBA the rest of the text on that line is a comment meant for human readers. We start our macro steps on the empty line.
What are the steps?
If we create a new paragraph with the current sentence manually, what steps would we take?
- Move the insertion point to the beginning of the sentence.
- Delete the extra space between the sentences.
- Press return to create the new paragraph.
But it’s just three steps, I hear you say, but they add up over the course of a novel. Plus it’s just nice not to have to do them.
Goto start of sentence
The obvious Word VBA command we can use for the first step is:
As previously mentioned, this command works with a list of document Units. We want the current sentence.
The standard MoveLeft command
might seem like a more natural command to use, but MoveLeft will move to the previous sentence if we’re already at the start of the current sentence.
StartOf stays at the beginning of the current sentence if we’re already there. That’s perfect for this macro.
At this point after the move command, the selection is just an insertion point (unless we purposefully use the Extend parameter like we’ve done before).
Delete extra space
Now delete the space between neighboring sentences. This is the most common use case for this macro.
We need to delete backwards, so we used -1 with the Count parameter. The default is to delete one character.
Remember if you’re a Mac user, the regular Delete command acts like a “forward delete,” deleting to the right in the document.
Insert the paragraph break
Enter the paragraph break using the Selection.
Collapse the selection
The InsertParagraph command actually expands the Selection to include the new paragraph, so we need to collapse back down to an insertion point.
Using the Direction parameter to collapse toward the end of the new Selection positions the insertion point at the new paragraph rather than at the end of the previous paragraph.
Finish the macro
That’s a few more commands than our regular newsletter content, but each step is easy to understand.
Now we just put the commands together.
I assigned this macro to Option+Return in Word for Mac or Alt+Return on Windows.
We consider the special case of what to do with the first sentence of the paragraph in the extended article content.
Any gotchas?
You should get into the habit of thinking what might cause unexpected results or maybe even problems when you use your macro. I’m not expecting you to think like a programmer, but it doesn’t hurt to be a little careful about special cases.
This is more of an issue for macros that delete or rearrange content. We definitely don’t want to lose a chunk of our work in progress because we didn’t think a little bit more when writing a macro.
For example, I was particularly careful when writing a macro to delete whole headings in one keystroke. Today’s macro is less of a concern since we only delete one character in the document.
Here are some special cases to think about today before rushing on to use our new productivity booster.
What about a starting selection?
A common check is thinking about whether the user runs the macro when text is already selected, whether intentionally or unintentionally. Sometimes our fingers just hit weird keys when we do something.
A nice side-effect of either suggested move command above is they both automatically collapse the selection unless we explicitly give the Extend parameter, so we don’t have to worry about whether the user starts with a selection or not in this macro.
This makes the new paragraph step safer, so we don’t accidentally wipe out any text when we insert the paragraph. (There are other commands to insert a paragraph while keeping a starting selection intact.)
What if you’re at the last sentence of a paragraph?
The last sentence (assuming it’s not the only sentence) of a paragraph has a preceding space just like a “middle” sentence does, so for this macro, there is no difference. Nothing bad could happen.
That’s nice.
What if you’re at the first sentence of a paragraph?
Seems like this “shouldn’t” happen since you wouldn’t use this macro if the current sentence was already at the start of a new paragraph. There’s no point in running the macro in that case.
But what if you hit the keyboard shortcut accidentally or out of habit as you type? You should at least be aware of what would happen.
What happens at the first sentence?
The Delete command was intended to delete the preceding space, but it will also delete the preceding paragraph marker if you start the macro when located somewhere inside the first sentence of a paragraph.
Then the InsertParagraph step will recreate the paragraph.
That seems fine … [ominous music plays in the background]
In fact, it will look like nothing happened in most cases, but at least nothing bad happens … right?
Only read this if you really want to know the details …
It's a minor side effect in this case (which is a good place to learn a lesson), but if you think a little more carefully, there could be an unintended consequence with the paragraph style.
If you’re creating a new paragraph from within or after the end of another, it will inherit that paragraph’s style (some paragraph styles will automatically create a new paragraph with a different style but that’s not important here).
This is expected in normal typing, but our macro would be joining the two paragraphs before separating them again.
In this case, the current paragraph will mysteriously inherit the style of the previous paragraph without anything else changing.
This isn’t horrible, but side effect might be a little aggravating six months from now after you’ve forgotten about it.
“Why did the whole paragraph suddenly become a heading1?”
First sentence correction
It’s kind of swatting at gnats, but for the perfectionists or hyper curious in the audience, let’s take care of this error, so the macro doesn’t mysteriously cause paragraphs to change styles.
It’s almost a case of the treatment being worse than the ailment, but we can still learn something in the process.
Detecting the start of the paragraph?
We need to detect whether the beginning of the sentence is at the same position as the beginning of the paragraph. How?
- Move the insertion point to the start of the current sentence just like before.
- If the Selection is at the same location as the Start of the paragraph, do nothing else and just let the macro end.
- Otherwise run the original macro steps to create the new paragraph with the sentence.
Seems simple enough.
Insertion point Start
How do we get the current insertion point position?
Use the Start position of the Selection.
Remember our Selection is collapsed to an insertion point at this stage of the macro, so Start and End have the same values.
Also this position is a literal character position in the document, so it's basically a number. Although, we don't really need to know the value specifically since we're comparing it to the paragraph start position.
Paragraph Start
How do we identify the start of the current paragraph?
Use the paragraph Range like we’ve done before but check the Start position (Ranges have Start and End positions like the Selection).
Then just compare the two values to determine if the insertion point is at the beginning of the paragraph.
Condition checks (If Then Else statements)
Remember an If Then Else allows us to check a condition and make a decision about whether to run certain macro steps based on whether the condition is true or false.
Recall it does the first part if the condition is true and the Else part if the condition is false. The else part is optional, in general.
Checking for the start of the paragraph
How does our start of the paragraph condition check look in VBA? We just compare the position values mentioned above.
Nothing happens in the first part which is a little awkward. Our useful macro steps all occur in the second “Else” part, so we should probably just exclude the first part and simplify the steps.
Conditional Not …
Since we only actually do something in the macro if the condition is not true, it’s more convenient to use a Not with the condition:
A Not before a condition “negates” it, so true is false and false is true. In this case, it allows us to do just the False part of the original condition and omit the original, unused True part for simplicity.
Using simpler commands is almost always the best way, and in this case, it’s also easier to read.
As an alternative, one could also just use a “not equal” symbol <> (i.e., less than or greater than means not equal). Both are useful at times.
Corrected version
That’s a lot of talking for a small issue, but the fool-proof version accounting for the first sentence of the paragraph is:
This slight improvement makes the macro look more complicated, but it’s not too much, I suppose. To be honest, I probably wouldn’t stop until I had fixed it like this.
Comment on comments
Notice how I explained the problem and reason for the extra check a little bit before the condition. This is nice to yourself if you ever come back to your macro since the reason for the extra check isn’t immediately obvious.
I had this happen to me while rewriting my own version of this macro while editing this article. I initially tried to remove what seems like several superfluous steps during the revision, but I eventually realized why I had added a note to myself (that I had ignored!). The gotcha turned out to be real.
While I did waste some time by ignoring my own comment, I would have wasted even more time re-solving the same issue that popped back up if I had not explained it to my future (now present) self as a comment.