Fix Word VBA Replacing Paragraph Marks Outside Selected Text
Question details
A Word VBA macro is replacing paragraph marks in the entire document rather than restricting the action to the highlighted selection.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Running a Find and Replace macro on text pasted from PDF files to remove unnecessary OCR paragraph marks.
- Observed behavior
- The macro ignores the selected text boundaries and replaces paragraph marks document-wide after reaching the first prompt.
Before modifying your macro, make sure to save a backup of your current VBA code or Word document to prevent accidental data loss.
Modify the Find Object Wrap Property to wdFindStop
Change the Wrap property in your VBA Find object so the macro stops at the end of the selection instead of asking to continue.
By default, recorded macros often set the .Wrap property to wdFindAsk or wdFindContinue. This causes the application to prompt you or automatically continue searching the rest of the document once it reaches the end of your highlighted selection. Setting it to wdFindStop confines the execution strictly to the selected range.
Open your document and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, find and open the module containing your Find and Replace macro.
Scroll through the code to find the 'With Selection.Find' block, and look for the line that says '.Wrap = wdFindAsk' or '.Wrap = wdFindContinue'.
Change that specific line to '.Wrap = wdFindStop' to prevent the search from extending beyond the highlighted text.
Save the macro, return to your document, highlight a new block of text, and run the macro again to verify it works correctly.

Try WPS Office for Seamless Document Editing and PDF Conversion
Struggling with messy formatting and complex VBA macros when copying from PDFs? WPS Office offers a lightweight, free alternative to Microsoft Office with a built-in PDF to Word converter that preserves formatting perfectly, eliminating the need for custom macros to clean up paragraph marks.
- 1. Download WPS Office: Visit the official WPS website to download and install WPS Office Free on your device.
- 2. Open PDF Files Directly: Launch WPS Office and open your PDF file directly within the application.
- 3. Convert to Word: Click on the 'PDF to Word' tool in the toolbar to cleanly convert your document without dealing with unwanted paragraph marks.

Frequently Asked Questions
Why does my Word macro replace text outside the highlighted selection?
This happens because the Find object's .Wrap property is likely set to wdFindAsk or wdFindContinue. This instructs the application to continue searching the rest of the document once it reaches the end of your current selection.
How do I remove paragraph marks from copied PDF text without a macro?
You can use the standard Find and Replace tool. Highlight the text, press Ctrl+H, enter '^p' (the code for a paragraph mark) in the 'Find what' box, enter a space in the 'Replace with' box, and click 'Replace All'.
What is the difference between wdFindStop and wdFindAsk in VBA?
wdFindStop forces the Find operation to terminate immediately when it reaches the end of the specified search range. wdFindAsk prompts the user with a dialog box asking if they want to continue searching the remainder of the document.
Does WPS Office support VBA macros?
Yes, WPS Office provides comprehensive VBA support in its premium versions, allowing you to run, edit, and create macros similarly to Microsoft Office, ensuring high compatibility for automated tasks.




