VBA & Macro Problems

How to Fix Excel VBA Run-Time Error 91 When Copying Data to Word

Huma Ashraf ChHuma Ashraf Ch Oct 29, 2026 870 views

Question details

The user needs to resolve run-time error 91 in Excel VBA, which occurs when attempting to copy a formatted range into a Word document using the Selection.Goto statement.

How to Fix Excel VBA Run-Time Error 91 When Copying Data to Word
Product
Microsoft Excel, Microsoft Word
Device & OS
not provided
Scenario
Automating cross-application tasks from Excel to Word using VBA macros.
Observed behavior
A run-time error 91 ('Object variable or With block variable not set') stops the macro execution because an object variable, specifically related to the Word document's Selection object, lacks a valid reference.
Before you start

Ensure your macro code has error handling temporarily disabled (e.g., commenting out 'On Error Resume Next') so you can accurately identify the exact line triggering Error 91.

Solution 1Recommended

Initialize Objects Correctly and Use ActiveWindow Selection

Correctly set up your Word application and document objects, and adjust your selection reference to target the document's active window.

Run-time error 91 occurs when an object variable evaluates to 'Nothing' or hasn't been properly assigned. When controlling Word from Excel, referencing the global 'Selection' object directly often fails because Excel does not know which application's Selection to use.

1
Initialize the Word Application Object

Use the 'Set' keyword to assign your Word application object variable. For example: Set wrdApp = CreateObject("Word.Application").

2
Open and Reference the Word Document

Assign the document to an object variable while opening it. Use syntax like: Set wrdDoc = wrdApp.Documents.Open("C:\Path\To\Your\File.docx"). Ensure the file path is absolutely correct.

3
Modify the Selection Statement

Instead of using wrdApp.Selection.Goto or simply Selection.Goto, explicitly reference the document's active window. Change your code to read wrdDoc.ActiveWindow.Selection.Goto.

Initialize Objects Correctly and Use ActiveWindow Selection
Reference Libraries: If you are using early binding, ensure you have checked 'Microsoft Word Object Library' in the VBA Editor under Tools > References.

Execute Your VBA Macros Flawlessly with WPS Spreadsheet

WPS Office provides robust support for VBA macros, allowing you to seamlessly automate tasks between spreadsheets and documents. Experience high compatibility with Microsoft Office scripts and formats for free.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office and ensure you select the version with VBA support enabled.
  2. 2. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your .xlsm file containing the cross-application macro.
  3. 3. Enable Macros and Run: Click 'Enable Macros' in the security warning prompt. Access the Developer tab, open the VBA Editor, and execute your code to copy the formatted range to WPS Writer.
Advanced VBA editor environment for seamless macro execution and debugging.Outstanding compatibility with Microsoft Excel (.xlsm) and Word (.docm) macro-enabled formats.Lightweight architecture ensures faster opening and processing of large automated tasks.
microsoft office alternative - wps office

Frequently Asked Questions

What exactly does 'Object variable or With block variable not set' mean in VBA?

This error (Error 91) means your VBA code is trying to use an object (like a Worksheet, Document, or Range) that hasn't been assigned an actual reference in memory using the 'Set' keyword.

Why does my code work with early binding but fail with late binding?

With early binding, you reference the Word library directly, giving you access to application-specific constants (like wdGoToBookmark). In late binding, these constants aren't recognized unless explicitly defined in your code, which can cause methods like Selection.Goto to fail.

Can Trust Center settings cause Run-time Error 91?

Generally, no. While Trust Center settings can block macros or add-ins entirely, Error 91 is specifically a coding issue related to uninitialized objects, not a security or trusted publisher block.