logo
search
VBA & Macro Problems

How to Fix Word VBA Code That Marks Correct Multiple-Choice Answers

WPS EditorWPS Editor Oct 1, 2026 868 views

Question details

The user needs to troubleshoot a Word VBA macro meant to grade multiple-choice quizzes by applying a border to correct answers, but the code throws paragraph-range expression errors and causes formatting misalignments.

Fix Word VBA Code That Marks Correct Multiple-Choice Answers
Product
Word
Device & OS
not provided
Scenario
Automating the grading of a multiple-choice quiz in a document by reading an answer key on the last page and highlighting the correct choices.
Observed behavior
The macro fails when processing paragraph-range expressions and applies misaligned formatting borders around the answers.
Before you start

Before modifying your VBA code, ensure you have saved a backup copy of your quiz document and have the Developer tab enabled to easily access the Visual Basic Editor.

Solution 1Recommended

Adjust the VBA Range Object to Exclude Paragraph Marks

Use this solution if your macro crashes on a paragraph-range expression. This usually happens because the macro attempts to format the hidden paragraph return character.

When looping through paragraphs in VBA, the default Range object includes the hidden paragraph mark (¶) at the end of the line. Attempting to apply certain specific borders or formatting to this hidden character often triggers a runtime error or causes formatting to stretch improperly.

1
Open the Visual Basic Editor

Press Alt + F11 to open the VBA editor and locate your quiz-grading macro.

2
Locate the failing Range definition

Find the line of code that sets the range for the multiple-choice answer, which might look like `Set rng = ActiveDocument.Paragraphs(i).Range`.

3
Exclude the paragraph character

Modify the range to exclude the final non-printable character by adding `rng.End = rng.End - 1` right before you apply the border.

4
Test the macro execution

Save your code and run the macro again to verify that it successfully selects and borders the text without throwing a range expression error.

Adjust the VBA Range Object to Exclude Paragraph Marks
Check Document Structure: Ensure your quiz document format is consistent. If answers are separated by line breaks (Shift+Enter) instead of paragraph breaks (Enter), you will need to adjust your parsing logic to look for Chr(11) instead of standard paragraph objects.
Free Microsoft Office alternative

Try WPS Office for Seamless Document Management and Macro Support

Having trouble executing complex Word macros? WPS Office provides a lightweight, highly compatible alternative to Microsoft Word. With robust VBA support in its advanced versions, it allows you to automate repetitive tasks, manage quizzes, and handle automated formatting effortlessly without the usual bugs.

  1. 1. Download and Install: Download WPS Office for free from the official website and follow the easy installation steps.
  2. 2. Open Your Quiz Document: Launch WPS Writer and open your macro-enabled document (.docm).
  3. 3. Enable Macros: Navigate to the Developer tab, enable macros, and run your automated grading script smoothly.
Fully compatible with Microsoft Word document formats (.docx, .docm)Advanced versions support VBA macros for custom automationFamiliar user interface requiring zero learning curveLightweight and fast performance even with heavy, macro-enabled documents
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Word VBA macro fail on a paragraph-range expression?

This commonly occurs when the defined VBA Range includes the non-printable paragraph mark (¶) at the end of a line. Certain properties and formatting commands cannot be applied to this character, causing a runtime error. You can fix this by shrinking the range by one character before applying changes.

How do I make my macro accurately read an answer key on the last page?

To reliably read an answer key, ensure the key is placed in a predictable structure, such as a dedicated table or a bookmarked section. Your VBA code can then target this specific object (e.g., `ActiveDocument.Bookmarks("AnswerKey").Range`) instead of relying on generic paragraph counts which may shift during editing.

Why are the borders drawn by my macro misaligned and too wide?

If you apply a border using `.ParagraphFormat.Borders`, Word will draw a box across the entire width of the page margins. To draw a snug box around just the text of the answer, you must apply the border to the text range directly using the `.Font.Borders` property.

Are Word VBA macros compatible with WPS Office?

Yes, WPS Office Pro natively supports VBA macros. You can open your `.docm` files in WPS Writer, and in most cases, your existing Microsoft Word macros will execute seamlessly thanks to the high level of API compatibility.