logo
search
Office Settings & Configuration

How to Fix Word VBA Error 5941 When Comparing Windows Side by Side

Adam DavisAdam Davis Oct 22, 2026 868 views

Question details

The user encounters VBA runtime error 5941 when running a macro to compare windows side by side.

How to Fix Word VBA Error 5941 When Comparing Windows Side by Side
Product
Microsoft Word
Device & OS
not provided
Scenario
Using a VBA macro to open a new view of the active document and compare both windows side-by-side using the Windows.CompareSideBySideWith method.
Observed behavior
The macro throws 'Runtime error 5941: The requested member of the collection does not exist' on the first execution. This happens because creating a new window changes the original document's caption (e.g., from 'sample.docx' to 'sample.docx:1' and 'sample.docx:2'), invalidating the string reference.
Before you start

Ensure you have saved your current VBA project and active documents before testing macros, as runtime errors can interrupt your workflow and leave Word in a split-screen state.

Solution 1Recommended

Use Window Object Variables Instead of Captions

Modify your VBA code to store a direct reference to the Window object rather than relying on its text caption, which changes dynamically.

When you use the NewWindow command, Word automatically appends instance numbers to the document's window captions (e.g., changing 'sample.docx' to 'sample.docx:1').

If your macro captured the caption 'sample.docx' as a string before opening the new window, passing that string to Windows.CompareSideBySideWith will trigger Error 5941 because 'sample.docx' no longer exists in the Windows collection. By using a Window object variable, the macro bypasses this string matching issue entirely.

1
Open the VBA Editor

Press Alt + F11 to open the Microsoft Visual Basic for Applications window, then locate the module containing your side-by-side macro.

2
Declare a Window Object Variable

Replace the string declaration (Dim s As String) with an object declaration: Dim origWin As Window.

3
Set the Variable to the Active Window

Before the NewWindow command, capture the original window by adding: Set origWin = ActiveWindow.

4
Execute the Side-by-Side Comparison

Run the new window command using ActiveDocument.ActiveWindow.NewWindow, then compare them using the object: Windows.CompareSideBySideWith origWin.

5
Disable Synchronous Scrolling

If you want the windows to scroll independently, leave the line Windows.SyncScrollingSideBySide = False at the end of your Sub routine.

Use Window Object Variables Instead of Captions
Macro Reliability: Using object references guarantees the macro will execute flawlessly on the first run without relying on unpredictable document caption strings.

Compare Documents Side by Side Easily in WPS Office

Instead of writing complex VBA macros to handle document views and risking runtime errors, WPS Writer provides a built-in, intuitive tool to view documents side by side natively.

  1. 1. Open Your Documents: Launch WPS Writer and open the two documents (or multiple views of one document) that you want to compare.
  2. 2. Navigate to the View Tab: Click on the 'View' tab located on the top ribbon of the application.
  3. 3. Activate Side-by-Side Mode: Click the 'View Side by Side' button to instantly snap your documents next to each other.
  4. 4. Toggle Synchronous Scrolling: Click the 'Synchronous Scrolling' button right below it to choose whether scrolling one document scrolls the other automatically.
Built-in 'View Side by Side' feature requiring absolutely no codingSeamlessly compatible with Microsoft Word document formats (.docx, .doc)Lightweight, fast, and free of VBA-related runtime errorsIntuitive user interface identical to classic Office designs
microsoft office alternative - wps office

Frequently Asked Questions

Why does Word rename my document window to filename:1 and filename:2?

Word assigns these numerical suffixes when multiple views or windows of the exact same document are open simultaneously. This helps the system and the user differentiate between the different views of the same file.

What does Runtime Error 5941 mean in Word VBA?

Runtime Error 5941, reading 'The requested member of the collection does not exist', occurs when your VBA code tries to interact with an item (like a Window, Bookmark, or Document) that is missing, closed, or incorrectly named in the specified collection.

How do I disable synchronous scrolling without using VBA?

If you are in Side-by-Side view, go to the 'View' tab on the ribbon and click the 'Synchronous Scrolling' button to toggle it off. This allows you to scroll through each document window independently.