Fix Cannot Find Name 'Office' Error in Excel Office Scripts
Question details
The user encounters a 'Cannot find name Office' error when trying to use Office.js UI methods inside an Excel Office Script.

- 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.
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.
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.
Navigate to the Automate tab on the Excel ribbon and click on your script to open the Code Editor.
Delete any lines containing `Office.context` or `Office.context.ui.displayDialogAsync` from your script.
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.
Click the Save button, then click Run to ensure the script executes without the 'Cannot find name' error.

Create an Office Add-in for Dialog Features
If your workflow absolutely requires user interface elements like pop-up dialogs, you must build an Office Add-in.
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. Download the Installer: Visit the official WPS Office website and click on the Free Download button.
- 2. Install WPS Office: Run the downloaded installer and follow the straightforward setup wizard.
- 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing .xlsx files to explore simpler macro automation options.

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.




