Fix Word VBA Macro That Changes Apostrophes to Quotes
Question details
The user needs to correct a VBA macro that improperly replaces apostrophes inside words with quotation marks or places closing quotes before punctuation.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA text-replacement macro to format quotes and apostrophes within a document.
- Observed behavior
- The macro lacks context sensitivity, causing it to blindly convert standard apostrophes used in contractions into double quotation marks.
Before modifying and running VBA macros, create a backup copy of your document to ensure original text formatting is preserved in case of an accidental bulk replacement.
Implement Context-Aware VBA Replacements
Modify the macro's replace function to look for specific character combinations, such as spaces before a quote, or exact ASCII values, to differentiate between apostrophes and quotes.
A standard search and replace in VBA treats all single quotes identically. To prevent apostrophes in contractions from being changed to double quotes, your script must evaluate the surrounding text.
By utilizing ASCII character codes like Chr(146) for smart single quotes and checking for adjacent spaces, the macro can accurately identify whether a character functions as an opening quote, closing quote, or an apostrophe.
Press Alt + F11 in Word to launch the Microsoft Visual Basic for Applications editor.
In the Project Explorer pane on the left, double-click the Module that contains your text replacement macro.
Modify the replace expression for opening quotes to look for a space followed by a single quote. For example: Replace(rg.Text, " '", smrtOpenDblQt).
Modify the replace expression for closing quotes by using the precise ASCII character code and checking for trailing punctuation or spaces. For example: Replace(rg.Text, Chr(146) & " ", smrtCloseDblQt).
Click the Save icon, close the editor, and run the macro on a small sample text to verify that apostrophes inside words remain unaffected.
Run and Debug VBA Macros Smoothly with WPS Writer
WPS Office provides robust developer tools and excellent compatibility with Microsoft Office formats, allowing you to troubleshoot and run text-replacement VBA macros effortlessly.
- 1. Install WPS Office: Download and install WPS Office, ensuring you select the version that supports VBA features.
- 2. Open Your Document: Launch WPS Writer and open your macro-enabled document (.docm).
- 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'VBA Editor'.
- 4. Edit and Execute: Paste your updated, context-aware macro code into the module and click 'Run' to format your quotes properly.

Frequently Asked Questions
Why does my Word macro confuse apostrophes with single quotes?
Word's auto-formatting often uses the same underlying character code (like a closing smart quote) for both an apostrophe and a single closing quotation mark. Unless your macro uses context clues—like spaces before or after the mark—it cannot distinguish between the two.
What is the ASCII code for a smart single quote or apostrophe?
While the standard straight single quote is ASCII 39, Word's 'smart quotes' typically use Chr(145) for an opening single quote and Chr(146) for a closing single quote or an apostrophe.
How do I temporarily disable smart quotes in Word VBA?
You can programmatically turn off smart quotes by adding 'Options.AutoFormatAsYouTypeReplaceQuotes = False' at the beginning of your macro. Remember to set it back to True at the end of your script to restore normal typing behavior.




