logo
search
VBA & Macro Problems

Fix Cannot Find Name 'Office' Error in Excel Office Scripts

Steve KSteve K Oct 8, 2026 869 views

Question details

The user encounters a 'Cannot find name Office' error when trying to use Office.js UI methods inside an Excel Office Script.

How to Fix 'Cannot Find Name Office' Error in Excel Office Scripts
Product
Microsoft Excel
Device & OS
not provided
Scenario
Attempting to display a pop-up dialog or use custom user-interface features within an Excel Office Script automation.
Observed behavior
The script execution fails, throwing a 'Cannot find name Office' error because Office Scripts do not support the Office.js namespace.
Before you start

Determine whether your automation goal strictly requires a custom user interface (like a pop-up dialog) or if the task can be achieved silently through background workbook modifications.

Solution 1Recommended

Remove Office.js Code and Use ExcelScript API

Office Scripts rely exclusively on the ExcelScript API for background automation and do not support the 'Office' namespace.

Excel Office Scripts are designed for silent, cross-platform background automation. Because they run independently of the user interface, APIs like Office.context.ui.displayDialogAsync are completely unavailable in this environment.

1
Open the Office Scripts Editor

Navigate to the Automate tab on the Excel ribbon and click on your script to open the Code Editor.

2
Remove Unsupported Methods

Delete any lines containing `Office.context` or `Office.context.ui.displayDialogAsync` from your script.

3
Implement ExcelScript Logic

Rewrite your automation logic using the standard `ExcelScript` interfaces, such as reading from or writing directly to a specific cell instead of prompting the user.

4
Save and Run

Click the Save button, then click Run to ensure the script executes without the 'Cannot find name' error.

Remove Office.js Code and Use ExcelScript API
API Differences: Remember that Office Scripts (TypeScript) and Office Add-ins (JavaScript/TypeScript) are two entirely different automation frameworks in Excel.
Free Microsoft Office alternative

Try WPS Office for Seamless and Free Spreadsheet Automation

If you find Microsoft Excel's dual script environments and add-in architectures overly complex, consider switching to WPS Office. It provides a lightweight, highly compatible spreadsheet tool with built-in JS Macro support for simpler automation.

  1. 1. Download the Installer: Visit the official WPS Office website and click on the Free Download button.
  2. 2. Install WPS Office: Run the downloaded installer and follow the straightforward setup wizard.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing .xlsx files to explore simpler macro automation options.
Highly compatible with Microsoft Excel formats (.xlsx, .xlsm, .csv)Built-in support for traditional VBA and JS Macros without complex add-in setupsFree, lightweight, and uses significantly fewer system resourcesFamiliar user interface with zero learning curve for Excel users
microsoft office alternative - wps office

Frequently Asked Questions

Why does 'Office.context.ui' work in some Excel examples but not in my Office Script?

Microsoft provides two distinct web automation environments. Office Add-ins use the Office.js library and support UI dialogs. Office Scripts use the ExcelScript API and run strictly in the background. Examples using 'Office.context' are meant exclusively for Add-ins.

Can I show a message box in an Excel Office Script?

No, standard Office Scripts (found in the Automate tab) do not support user interface pop-ups, message boxes, or dialogs. They are designed for silent automation. If you need user input, consider reading from a designated cell instead.

How can I migrate my Office Script to an Office Add-in?

You need to set up a web project using the Yeoman generator for Office Add-ins. You will then copy your core automation logic into the Add-in's function handlers, replacing ExcelScript API calls with their corresponding Office.js methods.

Does WPS Office support JavaScript macros?

Yes, WPS Office features a robust built-in JS Macro editor that allows you to automate tasks using JavaScript, providing a more integrated and accessible alternative to complex Office Add-in web development.