VBA & Macro Problems

How to Fix VBA InStr Returning Zero for Quotation Marks in Word

Emma BrownEmma Brown Nov 2, 2026 871 views

Question details

The user needs to find out why the VBA InStr function fails to locate quotation marks or apostrophes in a Word document.

How to Fix VBA InStr Returning Zero for Quotation Marks in Word
Product
Word
Device & OS
not provided
Scenario
Running a VBA macro loop using the InStr function to search for specific punctuation marks like quotation marks, apostrophes, commas, and grave accents.
Observed behavior
The InStr function returns zero (character not found) even though the targeted punctuation marks are visibly present in the document text.
Before you start

Ensure you understand how your word processor automatically formats typed text, as standard punctuation is often replaced with stylized alternatives instantly.

Solution 1Recommended

Update VBA Code to Check for Smart (Curly) Quotes

Modify your InStr function to search for both straight and curly punctuation character codes to account for Word's automatic formatting.

By default, word processors automatically convert straight quotes and apostrophes into smart (curly) quotes. The standard Chr(34) and Chr(39) only represent straight quotes, which causes the InStr function to fail when searching formatted text.

To fix this, you must expand your macro to look for character codes 145 through 148.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor and locate your macro script.

2
Locate the InStr function

Find the line of code where you are searching for Chr(34) (straight double quote) or Chr(39) (straight apostrophe).

3
Add curly double quotes conditions

Update your code to also search for left and right curly double quotes using Chr(147) and Chr(148).

4
Add curly single quotes conditions

Add search conditions for curly single quotes (apostrophes) by including Chr(145) and Chr(146) in your logic.

5
Run and verify

Run your macro again to verify that the InStr function now successfully returns the character positions for all punctuation types.

Update VBA Code to Check for Smart (Curly) Quotes
Additional Verification: You can also verify the comma by searching for Chr(44) and the grave accent with Chr(96) to ensure comprehensive punctuation matching in your code.

Write and Run VBA Macros Easily with WPS Office

WPS Office offers robust support for VBA macros, allowing you to automate tasks and run your customized scripts seamlessly. If you are experiencing formatting inconsistencies, writing and debugging your macros in WPS Writer is an excellent choice.

  1. 1. Enable Developer Tools: Open WPS Writer, navigate to the Options menu, and ensure the Developer Tools tab is enabled on your ribbon.
  2. 2. Access the VBA Editor: Click on the Developer tab and select 'Visual Basic' or press Alt + F11 to open the integrated VBA editor.
  3. 3. Write Your Macro: Insert a new module and paste your InStr macro code, ensuring you account for both straight and curly quote Chr codes.
  4. 4. Run and Test: Click the Run button or press F5 to execute your macro and verify it processes the document's punctuation correctly.
High compatibility with Microsoft Word documents (.doc, .docx, .docm) and standard VBA scripts.Accurate character and Chr code processing for precise InStr searches.Built-in developer tools for quick and efficient macro editing.
microsoft office alternative - wps office

Frequently Asked Questions

What are the VBA character codes for curly quotation marks?

In VBA, the left single curly quote is Chr(145), the right single curly quote (apostrophe) is Chr(146), the left double curly quote is Chr(147), and the right double curly quote is Chr(148).

Why does Word change my straight quotes to curly quotes automatically?

Word includes an AutoFormat As You Type feature designed to make documents look more professional. It automatically replaces standard keyboard straight quotes with typographically correct curly (smart) quotes as you type.

How can I replace all curly quotes with straight quotes using VBA?

You can use the Document.Content.Find.Execute method in your VBA script to search for Chr(146) or Chr(148) and replace them with Chr(39) or Chr(34). This ensures uniformity across your document before running other InStr searches.

Does the InStr function distinguish between different types of apostrophes?

Yes, the InStr function performs exact character matching. It treats a straight apostrophe (Chr(39)) and a smart apostrophe (Chr(146)) as entirely different characters, causing your search to return zero if the wrong type is targeted.