VBA & Macro Problems

Fix Word VBA Replacing Paragraph Marks Outside Selected Text

John WilsonJohn Wilson Oct 29, 2026 869 views

Question details

A Word VBA macro is replacing paragraph marks in the entire document rather than restricting the action to the highlighted selection.

How to Fix Word VBA Replacing Paragraph Marks Outside Selected Text
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 you start

Before modifying your macro, make sure to save a backup of your current VBA code or Word document to prevent accidental data loss.

Solution 1Recommended

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.

1
Open the VBA Editor

Open your document and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.

2
Locate your Macro

In the Project Explorer pane on the left, find and open the module containing your Find and Replace macro.

3
Find the Wrap Property

Scroll through the code to find the 'With Selection.Find' block, and look for the line that says '.Wrap = wdFindAsk' or '.Wrap = wdFindContinue'.

4
Change to wdFindStop

Change that specific line to '.Wrap = wdFindStop' to prevent the search from extending beyond the highlighted text.

5
Save and Test

Save the macro, return to your document, highlight a new block of text, and run the macro again to verify it works correctly.

Modify the Find Object Wrap Property to wdFindStop
Macro Updated: Your macro will now safely remove paragraph marks only within the text you have explicitly highlighted.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website to download and install WPS Office Free on your device.
  2. 2. Open PDF Files Directly: Launch WPS Office and open your PDF file directly within the application.
  3. 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.
Fully compatible with Microsoft Word (.docx) formatsBuilt-in PDF to Word converter prevents messy OCR paragraph breaksFamiliar user interface requires no steep learning curveSupports advanced Find and Replace functions without writing code
microsoft office alternative - wps office

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.