excel
Author and run VBA macros in workbooks via COM late binding, with a preflight trust check to prevent opaque execution failures.
Instructions
Purpose: Author and run VBA macros against Excel via COM late binding (ADR-015). Headline differentiator against Claude for Excel which writes formulas but cannot run VBA.
Details: action='run_vba' authors a Sub in a fresh workbook, saves into the managed Trusted Location (%LOCALAPPDATA%\desktop-touch-mcp\trusted-vba), and Application.Run the macro; the workbook is removed once the macro has run (workbookPath names where it ran from), so a macro that must keep results writes them to its own file, not ThisWorkbook. Requires HKCU AccessVBOM=1 + VBAWarnings=1 + a registered Trusted Location (all configured by node scripts/enable-access-vbom.mjs). Trust setup: Excel must restart after the CLI runs (values cached at process start). action='check_access_vbom' is a read-only preflight returning {trusted, lockedByPolicy, scope}.
Prefer: Run check_access_vbom first when a workflow depends on macro execution; the remediation hint pre-empts an opaque HRESULT 0x800a03ec failure inside run_vba.
Caveats: macroName MUST appear as Sub <name>(...) in code (else VbaMacroNotFound). VBA Editor UI is structurally bypassed — no UIA tree walk needed. Excel COM is STA: each call serialises through the bridge's worker thread, so long-running macros block subsequent excel() calls on the same MCP server. A runtime error in the macro fails the call with its VBA error number and text. A macro still running after timeoutSeconds (default 30 for a hidden run; a visible run only when given) has its Excel ended and fails the call: a compile error, MsgBox / InputBox, End or an endless loop would otherwise never return.
Examples:
excel({action:'check_access_vbom'}) → {trusted:true, scope:'hkcu'}
excel({action:'run_vba', code:'Sub DesktopTouchAdHoc()\n Range("A1").Value = "Hello"\nEnd Sub'}) → {ok:true, workbookPath:'...\trusted-vba\dt_vba_.xlsm'}
excel({action:'run_vba', code:'Sub Demo()\n MsgBox "hi"\nEnd Sub', macroName:'Demo', visible:true}) → demo recording path
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"action": "<action>"
},
"name": "excel"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | No | VBA source. MUST declare at least one Sub matching `macroName` (default `DesktopTouchAdHoc`). Example: "Sub DesktopTouchAdHoc()\r\n Range(\"A1\").Value = \"Hello\"\r\nEnd Sub" | |
| action | Yes | Action selector — one of: run_vba, check_access_vbom. Per-action required fields are enforced at call time (see the tool description); this flat schema lists every action's fields as optional. | |
| include | No | Optional response-shape opt-in. `['envelope']` returns the self-documenting envelope (`_version` / `data` / `as_of` / `confidence`). `['raw']` forces raw shape (overrides DESKTOP_TOUCH_ENVELOPE=1 server default). Default behaviour is raw shape (compat with existing clients). | |
| visible | No | If true, show the Excel window during execution. Default false (headless). Setting visible:true is useful for demo recording but may surface MsgBox/InputBox calls in the macro that block the COM thread. | |
| macroName | No | Sub name to invoke. MUST appear in `code` as `Sub <name>(...)`. Default `DesktopTouchAdHoc`. | DesktopTouchAdHoc |
| timeoutSeconds | No | How long the macro may run. Past it, this run's Excel is ended (unsaved work in it is lost) and the call fails with VbaMacroExecutionFailed: a VBA dialog (a compile error, MsgBox, InputBox), `End`, or an endless loop would otherwise keep the call from returning. Default 30 for a hidden run; a visible run is not timed unless this is given. |