VBA & Macro Problems

Fix Word VBA Macro That Changes Apostrophes to Quotes

Anum MushtaqAnum Mushtaq Oct 23, 2026 870 views

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.

How to Fix a Word VBA Macro That Changes Apostrophes to Quotes
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Word to launch the Microsoft Visual Basic for Applications editor.

2
Locate Your Macro

In the Project Explorer pane on the left, double-click the Module that contains your text replacement macro.

3
Update the Opening Quote Logic

Modify the replace expression for opening quotes to look for a space followed by a single quote. For example: Replace(rg.Text, " '", smrtOpenDblQt).

4
Update the Closing Quote Logic

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).

5
Save and Test

Click the Save icon, close the editor, and run the macro on a small sample text to verify that apostrophes inside words remain unaffected.

Debugging Tip: Use the F8 key in the VBA editor to step through your code line by line. This allows you to watch how the text string changes and verify the logic before executing the entire script.
Efficient Text Processing

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. 1. Install WPS Office: Download and install WPS Office, ensuring you select the version that supports VBA features.
  2. 2. Open Your Document: Launch WPS Writer and open your macro-enabled document (.docm).
  3. 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'VBA Editor'.
  4. 4. Edit and Execute: Paste your updated, context-aware macro code into the module and click 'Run' to format your quotes properly.
High compatibility with Microsoft Word macro-enabled documents (.docm)Built-in Developer tools for editing and testing VBA scriptsLightweight application ensures fast document processingFamiliar interface requires zero learning curve
microsoft office alternative - wps office

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.