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

Define quote characters as global constants

Word • Macros • Features
Peter Ronhovde
7
min read

We define double and single quote characters as VBA constants. Quote characters are used often in various editing macros, so defining them as global constants saves us from defining them in each macro where they're used.

Thanks for your interest

This content is part of a paid plan.

Define constants for all macros

In the context of writing and editing novels, certain text and characters are useful across many macros, but it is annoying to redefine them every time they're needed. VBA will not allow the constants to change anywhere, but if the values are fixed, why not define them once and be done with it?

Fortunately, the main difference between a constant that only works inside a specific macro and a global constant that works with any macro is simply where we define them in the macro file (called a "module"). If we create constants at the top of the module in the VBA editor, we can use them in any macro within that module (see the slightly more general explanation in a separate article).

Notation to declare a constant

The more general global constants article goes into more detail about the overall process (it's not hard). How do we define typical VBA constants in a nutshell?

Const SomeConstant As SomeType ' Not done ...

After the Const keyword, give a unique, descriptive name followed by "As" and a valid data type. A name cannot be a VBA keyword like Const, String, and others. They may only contain alphabetic (as in A – Z or a – z), numeric characters (0 – 9), or an underscore _ to simulate a space since spaces are not allowed.

Valid constant data types include those with possible fixed values like numbers or text. The relevant type for this article is a String (for the plain text punctuation), but numeric types like Integer, Long (allows bigger integers), or Boolean (True or False values), and a few others also work.

Defining constant values

Constants cannot changed after being created since they're constant by definition. For example, VBA will pop up an error message if we try to do the following:

' Cannot change a constant after it is declared because it is fixed
Const FirstName As String ' Not complete ...
FirstName = "Harold" ' Causes an error with a constant

When we declare the constant without giving it an initial value, VBA automatically assigns a default empty string "" value (literally a string with no text inside it). The next line attempts to reassign the constant to something more useful, but it causes an error because the constant cannot change after it is created.

Assign a constant value when it is created

We must define the value at the same time we declare the constant. Immediately after the data type, just include a variation of = "Some text", but the value must be fixed.

Const FirstName As String = "Harold" ' Valid constant definition

One could argue that "Harold" is already a constant string, so why define it separately as new named constant. The idea is to use it seamlessly across multiple macros, but we're setting up for some fixed punctuation marks below.

Constants can refer to prior constants

Constant values can refer to a previously defined constants which is helpful at times.

Const LastName As String = "Smith"
' Valid constant definition based on other constants
Const FullName As String = FirstName + " " + LastName

This allows us to build more complicated constants or variations of them as needed.

Double and single quote characters

Historically, typesetters introduced ideas of quotes as upside down commas in the 16th century to make quoted text clearer. The marks were originally placed in the margin, but the characters evolved over the period, particularly in the 18th century, eventually placing them around the quoted text as we do today.

Plain text character constant issues

Quotations included straight, left, and right for both single and double quote variations; but early typewriters only included only straight single and double quotes. Modern keyboards inherited the limitation with limited key space playing a factor more than mechanical issues. In modern word processing, the curly quotes are standardized characters (although different fonts have variations), so Word converts straight quotes into the respective curly version automatically.

We need to be specific in writing and editing macros since Word will not convert the quotes for us inside a macro. It's actually an AutoCorrect change during real-time writing, and Word sees the different quotes as distinct characters anyhow (it does not recognize the visual similarity like our human brain does). Our macros should detect and manipulate either form of quotes as needed.

Cannot use character functions

We've used character functions like ChrW(8220) or ChrW(8221) multiple times for double quotes to keep the articles more self-contained, but functions aren't allow when declaring constants. ChrW kind of feels like it generates a constant value since the referenced characters almost never change, but it is nevertheless a function which must be evaluated by VBA.

Quotes within quotes …

It's a little more troublesome because VBA represents a text string using straight double quotes as in "abc," so how do we represent double quotes with double quotes? It's a pickle conundrum.

Fortunately, we don't need to solve the problem from scratch. We can simply twiddle around with smart quotes in Word and then copy and paste the resulting characters over to our new VBA constants, but the declarations below save even that small effort.

Easy quote constants

How do we define quote constants and make them easy to use?

  • A straight single quote is easy. Since it isn't a double quote, we can just include it in double quotes like other plain text characters "'".
  • We can also specify a straight double quote as two double quotes inside another pair of double quotes """". That’s not confusing at all?

I suppose the double quote notation isn't bad once you get used to it, but it just raises my hackles real good. However, we're stuck with it for a constant declaration because we must assign a fixed value to the constant below. Unfortunately, neither of those examples includes curly left and right versions of single or double quotes.

Define the quote characters

Double quotes appear by the thousands in a novel, so when working with them in macros, it would be convenient to be able to refer to them with easily remembered names like LeftDQ as opposed to the much more obscure ChrW(8220).

After some tinkering around in Word, we can copy and paste the respective fixed characters over into a macro module. The quote character constants are:

' Define quote character global constants using fixed characters
Const LeftDQ As String = "“"
Const RightDQ As String = "”"
Const StraightDQ As String = """"
Const LeftSQ As String = "‘"
Const RightSQ As String = "’"
Const StraightSQ As String = "'"

While a human eye might squint at the distinctions between the above constants, a computer sees the different quotes as distinct characters. The main problem is the straight double quote which uses a special VBA notation of four double quotes (really a pair of double quotes inside double quotes).

Just copy and paste these constants into your current macro module (the editor window that probably pops up when you tap Option+F11 in Word for Mac or Alt+F11 in Windows). To work for any macro (be global), they must be at the top of the module before any macros are defined.

Adding quote character constants

Since we're already defining a bunch of constant characters, we might think ahead about related constants. For example, some dialog editing macros refer to any variation of a double quote character, so we could go ahead and define all three together in a separate constant.

' Define quote character group as a string constant
Const DoubleQuotes As String = LeftDQ + RightDQ + StraightDQ

Such character strings could be useful with move methods that require a target character set. Define whatever groups are convenient.

Any other useful macro constants?

Any other author-esque constant ideas? The list isn't long, but the others are outside the scope of this article, so see the more general version if you're interested.

Project global (Public) constants

If we want our constants to be available to all modules in the project (essentially all separate macro files in a document or template), just prepend the keyword Public to the the constant definition.

' Example public constants available to all modules in a project
Public Const LeftDQ As String = "“"
Public Const RightDQ As String = "”"
' And so on ...

Quote characters are essentially universal constants even outside of VBA, so it makes sense to declare them across the whole project at the same time.

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.