Fix Word 365 VBA Macro Crashing on Selection.Expand wdLine
Question details
The user needs a reliable way to search for specific text and delete the containing line using a Word VBA macro without causing the application to crash.

- Product
- Microsoft Word 365
- Device & OS
- Windows
- Scenario
- Running a VBA macro to find specific text and delete the line containing it after a recent Windows update.
- Observed behavior
- The VBA macro crashes Word entirely when using 'Selection.Expand Unit:=wdLine' to select and delete complete lines.
Ensure you have saved a backup of your Word document and verify that your Microsoft Office suite and Windows operating system are fully updated before executing or modifying VBA scripts.
Use a Range Object to Search and Delete Lines
Replacing the Selection object with a Range object provides a much more stable and reliable execution method, bypassing visual selection-related crashes entirely.
Using a Range object is highly recommended in VBA programming because it manipulates text purely in the background. It does not move the visible cursor or physically select UI elements in the document window, which significantly reduces instability caused by rendering engine conflicts during Windows updates.
Open your Word document and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications editor.
In the left panel (Project Explorer), double-click the module containing your macro to view the code.
Replace your 'Selection' code by first declaring a Range variable at the top of your subroutine: type 'Dim oRng As Range', then set it to target the document by typing 'Set oRng = ActiveDocument.Range'.
Create a 'With oRng.Find' block. Set '.Text = "Your Target Text"'. Inside a 'While .Execute' loop, use the built-in bookmark to delete the line by typing: 'oRng.Bookmarks("\Line").Range.Delete'.
Save your script and press 'F5' to run the macro. Test it on a sample document to confirm that the text lines are deleted without causing Word to crash.

Change Selection Parameter to wdParagraph
If you must keep the Selection method, expanding the selection to the entire paragraph can temporarily bypass the specific wdLine crash bug.
Experience Stable Document Editing with WPS Office
If you are frustrated by sudden application crashes, macro instability, or erratic behavior after software updates in Microsoft Word, consider switching to WPS Office. It provides a lightweight, highly compatible, and user-friendly environment for all your document needs.
- 1. Download WPS Office: Visit the official WPS website and click the free download button to get the latest installation package.
- 2. Install the Software: Run the downloaded installer and follow the quick on-screen instructions to set up WPS Office on your computer.
- 3. Open Your Documents: Launch WPS Writer and open your existing .docx files to enjoy stable, crash-free document editing.

Frequently Asked Questions
Why does Selection.Expand Unit:=wdLine cause Word to crash?
Crashes related to 'wdLine' often occur after certain Windows or Office updates that interfere with how the Word rendering engine handles physical screen layout selections. Because 'Selection' requires active screen updating, it is more prone to system-level interruptions and display driver conflicts.
What is the difference between Selection and Range in VBA?
The 'Selection' object represents the currently highlighted area or cursor position in the actual user interface. A 'Range' object is an invisible, programmatic mapping of a document area. Because 'Range' works in the background without updating the UI, it is faster and much less prone to application crashes.
How do I use predefined bookmarks like '\Line' in Word VBA?
Word has several built-in, hidden bookmarks (like '\Line', '\Page', and '\Doc'). You can invoke them using the Bookmarks collection on an existing Range or Selection. For example, 'oRng.Bookmarks("\Line").Range' instantly captures the specific line where the current Range is located, allowing you to delete or modify it easily.
Does WPS Office support VBA macros?
Yes, the advanced and business versions of WPS Office include support for VBA (Visual Basic for Applications). This allows you to run, edit, and create custom scripts to automate tasks seamlessly, providing an excellent alternative to Microsoft Office.




