Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Adding Smart Quotes through Macro Text.

Adding Smart Quotes through Macro Text

Written by Allen Wyatt (last updated February 24, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


3

Mary uses macros quite a bit to add text to her documents. The problem is that if the added text contains apostrophes, those are added to the text as "straight" rather than "smart." She indicates that she has the AutoFormat As You Type setting in place that tells Word to use smart quotes instead of straight quotes, but that has no affect on the text inserted by my macros.

When you insert text into a document by using a macro rather than typing, the text in each case is handled differently by Word. Text that you type is processed as each character is entered. Text that is inserted by a macro is treated more like text that is pasted into a document. Thus, if you type "this is my text," Word does its processing after each and every character. That means there is time for the program to check AutoFormatting and AutoCorrect and all the rest of the things that Word does to process text.

When you use a macro to enter the same text, it is inserted as a block, as if you pasted it in place. This means that any characters in the middle of the text (such as quotes or apostrophes) that would have been processed by AutoFormatting are not "caught" and processed. This means that straight quotes are not changed to smart quotes if they are contained within text that is inserted by your macro.

There are a couple of ways you can approach a solution to this. The first is to have your macro, after it inserts all your text, do a find and replace operation to replace all quotes with quotes and apostrophes with apostrophes. This may sound strange, but if you have AutoFormat As You Type set to use smart quotes, the find and replace operation will end up changing the straight quotes to smart quotes.

This approach is the way to go if your macro inserted lots of text in the document. If it is inserting smaller chunks of text, then it is easier to make sure that the macro is inserting the correct ASCII codes for smart quotes to begin with. The ASCII codes for a regular quote is 34, but a smart opening quote has a code of 147 and a closing quote is 148. There are similar differences in the codes used for apostrophes. If you use the Chr function to insert the proper character, you will always have the quotes you want.

One way to do that is to use code similar to the following near the beginning of your macro:

If Options.AutoFormatAsYouTypeReplaceQuotes = True Then
    sAposOpen = Chr(145)
    sAposClose = Chr(146)
    sQuoteOpen = Chr(147)
    sQuoteClose = Chr(148)
Else
    sAposOpen = Chr(39)
    sAposClose = Chr(39)
    sQuoteOpen = Chr(34)
    sQuoteClose = Chr(34)
End If

This code checks to see if the AutoFormat As You Type setting is turned on for smart quotes. If it is, then the four variables are set to the proper ASCII codes for smart quotes. If it is not turned on, then the variables are set to the proper codes for straight quotes. You can then later use these variables in your macro as you assemble and insert text. For instance, if you want to insert the text "my brother's house is down the street," you could insert it in this manner:

sMyString = "my brother" & sAposClose & "s house is down the street"
Selection.InsertAfter " " & sMyString

If you find this approach bothersome (breaking up your strings in this manner), then there is one other option. You can create your own function that does the proper replacements in your strings at one time. The following macro will do the job nicely:

Function RepQuotes(sRaw As String) As String
    Dim sTemp As String
    Dim sAposOpen As String
    Dim sAposClose As String
    Dim sQuoteOpen As String
    Dim sQuoteClose As String

    If Options.AutoFormatAsYouTypeReplaceQuotes = True Then
        sAposOpen = Chr(145)
        sAposClose = Chr(146)
        sQuoteOpen = Chr(147)
        sQuoteClose = Chr(148)
    Else
        sAposOpen = Chr(39)
        sAposClose = Chr(39)
        sQuoteOpen = Chr(34)
        sQuoteClose = Chr(34)
    End If

    sTemp = Replace(sRaw, " " & Chr(39), sAposOpen)
    sTemp = Replace(sTemp, Chr(39), sAposClose)
    sTemp = Replace(sTemp, " " & Chr(34), sQuoteOpen)
    sTemp = Replace(sTemp, Chr(34), sQuoteClose)
    RepQuotes = sTemp
End Function

What you do is to construct your text strings as normal, and then pass them through the RepQuotes macro. The macro determines the proper quotes to use and then does the conversions. It determines whether a quote is an opening quote or ending quote in the text by whether there is a space before the quote or not. If there is, it is assumed to be an opening quote; if not, then it is a closing quote.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11833) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Adding Smart Quotes through Macro Text.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Protecting Hidden Text

Formatting some of your text as hidden can be a great help when you need to keep some things from being viewed or ...

Discover More

Counting Precedents and Dependents

Do you need to know how many precedents or dependents there are on a worksheet? You could count them manually, or you ...

Discover More

Adding Ordinal Notation to Dates

Want to add an ordinal suffix to a number, as in 2nd, 3rd, or 4th? Excel doesn't provide a way to do it automatically, ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Repaginating in a Macro

When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the ...

Discover More

Accessing a Footnote Number in VBA

If you are working with a document that includes footnotes, you might use a macro to do some processing of that document. ...

Discover More

Testing if Word is Running on a Windows or Mac System

When creating macros for others to use, it can be helpful to know whether those others are running the macro on a Windows ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2024-02-26 23:49:48

Tomek

@ Andrew:
I didn't know it. You can learn something every day!
Interesting, and good. I checked it and it works as you described.
I guess, this is for cases where you have just two digits for a year and the apostrophe indicates omitted part.
I found that it only works for two digits, not one, or three. or more, so this is good. But the apostrophe is only converted to closing when you type a space, tab, or punctuation that follows the two digits. Not when there is end of paragraph or manual line break, which should work the same.


2024-02-26 16:36:58

Andrew

Not quite - you'll see that '24 (for example} will convert the apostrophe, which follows whitespace, will "smarten" to a closing single quote - which is correct.


2024-02-25 01:11:43

Tomek

Are you curious how Word determines which smart quote/apostrophe (open or close) to use?

It seems that if there is white space before a quote/apostrophe then the opening one will be used. If it directly follows other characters, then the closing one will be used. There is no pairing of the quotes.

Hence beware, if you include trailing spaces in your quote, it will end with an opening quote/apostrophe.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.