Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Detect punctuation mark type

Word • Macros • Functions
Peter Ronhovde
10
min read

This function identifies sentence punctuation mark categories to expedite making text spacing decisions in other macros. Specifically, we identify whether a punctuation mark is normally preceded by a space or paragraph mark or ends a sentence.

Thanks for your interest

This content is part of a paid plan.

Detect punctuation mark type

Some VBA functions are short, but they make things more convenient when writing macros. They help keep our main macros less cluttered with copies of repeated commands to accomplish common tasks.

A task that pops up occasionally is to check whether a particular character is a punctuation mark. More specifically in Word macros, we often make text spacing adjustments based on the context of our document, so we need to distinguish between different types of punctuation marks.

Punctuation categories

For our functions today, the various types of punctuation marks include:

  • Sentence ending
  • Usually preceded by a space or paragraph mark
  • Usually not preceded by a space or paragraph mark

Since we don’t have any specific terminology, we’ll refer to the second group as “left” punctuation marks in analogy with left single or double quotes. We’ll call punctuation marks that are usually not preceded by a space or paragraph mark a “right” punctuation mark.

The main function covered below will focus on the last case, but the extended content shows simple variations to detect the other two cases. Of course, the first category overlaps with the last. Some characters are omitted because they are not used often, or their typical text spacing is not consistent.

What’s the plan?

Our task is simple. We literally compare a given character to a list of specific punctuation marks: ? .  ; , ! : ; ] ) right single quote, right double quote, en-dash, or em-dash, and others depending on which question we’re asking. We then return whether we find a match.

Create function skeleton

We start by creating the function skeleton for the last case above.

Function IsRightPunctuation(sPunctuation As String) As Boolean
' Include function step

IsRightPunctuation = False ' Temporary value
End Function

The return value will be modified below.

Return type

We’ll return a True or False (Boolean) value based on whether the character matches any punctuation marks in the list.

What parameters to use?

We accept a String parameter sPunctuation corresponding to the single-character punctuation mark to check.

Get first character

A general String (plain text) variable can have more than one character. While there is a way to declare a fixed-length string, it doesn’t play nice as a function parameter in VBA, so we’ll manually look at the first character of the given punctuation string.

We use a standard string function Left(…) that returns a given number of characters NCharacters from the left side of a string.

Left(SomeString, NCharacters)

We pick out the first character in our given sPunctuation variable and store it in a variable c.

c = Left(sPunctuation, 1)

Any other characters after the first are ignored. For example, if someone called the function using the string “;abcdef” then after this command, the variable c would store just the semicolon “;”.

I often use more descriptive variable names, but here we have a long chain of comparisons to check for punctuation matches, so I made the variable name super short.

Return comparison result

Returning a value in VBA is the same as assigning a value to the function name.

Single character example

If we check versus a single character, such as a comma, we return a True or False (Boolean) value based on whether it literally equals = a comma “,”.

IsRightPunctuation = (c = ",")

The double quotes around the comma “,” are required when indicating a character is a text string.

The parentheses aren’t strictly necessary, but I like to put them around the comparison to clearly separate it from the variable assignment. Unfortunately, the respective equals signs mean two different things. The left = is a value assignment to the function variable name, but the right one indicates a logical True-False comparison.

Arghhh. I don’t like this notation inconsistency in VBA, but we’re stuck with it.

Sentence ending punctuation

We can include more characters in the comparison, but we want any of them to give us a True result. That sounds like an Or operation (see another article for more explanation).

For sentence ending punctuation (see extended content), the returned value would be:

IsSentenceEndPunctuation = (c = "." Or c = "?" Or c = "!")

Right punctuation marks

We also need a version for right punctuation marks (those not usually preceded by a space). There are too many to include everything, so we include the more common ones. Even then, the list is a little long. Regardless, we chain all the choices together with Or operators.

IsRightPunctuation = (c = "," Or c = "?" Or c = "." Or c = "!" Or _
c = ":" Or c = ";" Or c = "]" Or c = ")" Or c = RightSQ Or _
c = RightDQ Or c = "-" Or c = EnDash Or c = EmDash)

When a command exceeds a line length, we can continue the line using an underscore _ at the end. There is a limit to how many continued lines, but it’s not usually a problem.

Some special characters are system dependent where character numbers change between Word for Mac or Windows, so several cases above use functions defined in a previous article.

Some style guides have varied rules such as spacing around a dash in a sentence, so add or remove characters as needed.

Unfortunately, VBA does not short circuit logical conditions like this (i.e., stop as soon as it finds a True result) like some other languages, so it should probably not be used in a time-intensive portion of a macro unless necessary.

Final Functions

Putting everything together, the related functions are:

Detect right punctuation marks

Detect whether the given character is a punctuation mark not usually preceded by a space. We called these “right” punctuation marks.

Function IsRightPunctuation(sPunctuation As String) As Boolean
' Determine whether given string is a punctuation mark usually not preceded
' by a space or paragraph mark
' Only checks first character of sPunctuation
' Cases: ? . ; , ! : ; ] ) [Right]' [Right]" - [EnDash] and [EmDash]

' Get first character c
c = Left(sPunctuation,1)
IsRightPunctuation = (c = "," Or c = "?" Or c = "." Or c = "!" Or _
c = ":" Or c = ";" Or c = "]" Or c = ")" Or c = RightSQ Or _
c = RightDQ Or c = "-" Or c = EnDash Or c = EmDash)
End Function

I seem to need this version more often in my own macros. Of course, add or remove relevant punctuation as desired.

VBA always performs the entire conditional logic even if it finds an early match. For typical Word macros, you’ll never notice the difference, but be careful using it in time-intensive loops.

We included some other characters that are system dependent through the included functions: RightSQ, RightDQ, EnDash, and EmDash. We left out a straight double quote [as c = Chr(34)] or straight single quote [as c = Chr(39)] since both are ambiguous whether they mark the beginning or end of a quotation.

Variations

For completeness we include two quick variations that may be useful at other times.

Detect left punctuation marks

We called “left” punctuation marks those that are normally preceded by a space or paragraph mark.

Function IsLeftPunctuation(sPunctuation As String) As Boolean
' Determine whether given string is a punctuation mark usually preceded by a space
' or paragraph mark
' Only checks first character of sPunctuation
' Cases: ( [ [Left]' [Left]" $

' Get first character c
c = Left(sPunctuation,1)
IsLeftPunctuation = (c = "(" Or c = "[" Or c = LeftSQ Or c = LeftDQ Or c = "$")
End Function

We left out a straight double quote since it’s ambiguous whether it is the beginning or end of a quotation. We include some other characters that are system dependent through the functions LeftSQ and LeftDQ (see previous article).

These are not included in the function, but the characters for inverted question mark or exclamation point are Chr(192) and Chr(193), respectively, in Word for Mac. In Word for Windows, the respective characters are Chr(161) and Chr(191).

Detect end of sentence punctuation marks

Another obvious case is checking for whether the punctuation marks the end of a sentence, so we tweak the previous macro for this specific case.

Function IsSentenceEndPunctuation(sPunctuation As String) As Boolean
' Determine whether given string starts with an ending sentence punctuation mark
' Only checks first character of sPunctuation
' Cases: ? . and !

' Get first character c
c = Left(sPunctuation,1)
IsSentenceEndPunctuation = (c = "?" Or c = "." Or c = "!")
End Function

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.