VBA & Macro Problems

Fix Word 365 VBA Macro Crashing on Selection.Expand wdLine

Rana GarciaRana Garcia Oct 19, 2026 868 views

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.

Fix Word 365 VBA Macro Crashing on Selection.Expand wdLine
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Open your Word document and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications editor.

2
Locate Your Macro Code

In the left panel (Project Explorer), double-click the module containing your macro to view the code.

3
Declare and Set the Range Variable

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'.

4
Implement the Range Find and Delete Loop

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'.

5
Test the Updated Macro

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.

Use a Range Object to Search and Delete Lines
Performance Improvement: In addition to fixing the crash, a Range-based macro executes significantly faster than a Selection-based macro, especially in large documents.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and click the free download button to get the latest installation package.
  2. 2. Install the Software: Run the downloaded installer and follow the quick on-screen instructions to set up WPS Office on your computer.
  3. 3. Open Your Documents: Launch WPS Writer and open your existing .docx files to enjoy stable, crash-free document editing.
Full format compatibility with Microsoft Word (.docx), Excel, and PowerPoint files.Lightweight installation that runs smoothly without consuming excessive system resources, preventing heavy crashes.Familiar user interface ensuring a seamless migration from Microsoft Office.Advanced document editing capabilities designed with robust stability in mind.
microsoft office alternative - wps office

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.