Metadata-Version: 2.4
Name: vba
Version: 0.1.2
Summary: Read, change, debug and install VBA for Word, Excel and PowerPoint, as a command line and an MCP server
Keywords: vba,macro,word,excel,powerpoint,office,mcp
Author: Keyor
License-Expression: AGPL-3.0-or-later
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Office/Business :: Office Suites
Classifier: Programming Language :: Python :: 3
Requires-Dist: pyopenvba==6.6.0
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/superkeyor/vba
Description-Content-Type: text/markdown

# vba

Read, change, debug and install VBA for Word, Excel and PowerPoint, from the command line or an MCP client.

- Reads and writes the macros inside Office files with [pyOpenVBA](https://pypi.org/project/pyopenvba/). No Office needed.
- On a Mac, loads changed code into the running app without a restart and runs it on one file.
- Installs finished code in your macro file: backed up, checked, and only while the app is closed.

## What works where

| | Mac | Windows | Linux |
|---|---|---|---|
| `list`, `pull`, `install` | Yes | Yes, but not yet tried on a real PC | Same, with `--file` |
| `load`, `run`, `exec`, `unload`, `status` | Yes | No | No |
| Default macro files | Found | Looked for in `%APPDATA%\Microsoft` | None |
| `mcp` | All tools | `vba_help`, `vba_list`, `vba_pull` | `vba_help`, `vba_list`, `vba_pull` |

Live control uses AppleScript, so it is Mac only; see [To do](#to-do) for Windows.
Until then, on Windows: test a change in the VBA editor, then `install` it with the app closed.

## Run

```bash
uvx vba --help                        # needs only uv
uvx --from /path/to/vba vba --help    # from a checkout
```

`vba --help` is the reference for commands, defaults and limits.

## MCP

```json
{
    "command": "uvx",
    "args": ["vba", "mcp"]
}
```

The server runs VBA in your open Office apps, so a client can do whatever VBA can.
It has no `install`, and an Excel file given as `doc` is copied first; other files the code opens are not.

## How live loading works

| | Dev add-in | Loaded by |
|---|---|---|
| Word | `VBADev.dotm` in the Word startup folder | AppleScript `add addin` |
| Excel | `VBADev.xlam` in the Add-Ins folder | A loader add-in in the Excel startup folder |
| PowerPoint | `VBADev.ppam` in the Add-Ins folder | AppleScript `register add in` |

An app reads an add-in only when it loads it, so each update is unload, replace, load.
`run` and `exec` add a caller to the code that checks the target file, catches errors and returns a function's value.

### Where the files go

`OFFICE` is `~/Library/Group Containers/UBF8T346G9.Office`, and `CONTENT` is `OFFICE/User Content.localized`.
`HOME` is the data folder in note 7 of `vba --help`.

| File | Location | Removed |
|---|---|---|
| Word dev add-in | `CONTENT/Startup.localized/Word/VBADev.dotm` | By `unload`, or at the end of `exec` |
| Excel dev add-in | `CONTENT/Add-Ins.localized/VBADev.xlam` | Same |
| PowerPoint dev add-in | `CONTENT/Add-Ins.localized/VBADev.ppam` | Same |
| Excel loader | `CONTENT/Startup.localized/Excel/VBALoader.xlam` | By `vba excel unload --loader`, with Excel closed |
| Loaded code | `HOME/dev/APP.bas` | By `unload`, or at the end of `exec` |
| Macro result | `OFFICE/vba_result.txt` | After each run |
| Excel copies of `--doc` | `HOME/copies/NAME-TIME.xlsx` | Never |
| `install` backups | `HOME/backups/NAME-TIME.EXT` | Never |
| Files being read or built | System temp folder | When the step ends |

`--new` opens an unsaved file. If `exec` stops on a dialog or a timeout, the add-in stays until `unload`.

### Prompts

- The tool's own add-ins should load without an Enable Macros prompt.
  Excel would prompt for an ordinary open, so the loader uses `AddIns`.
- A document that can hold macros (such as `.docm`, `.xlsm`, `.xlsb`, `.pptm`, or the older `.doc`, `.xls` and `.ppt`) may prompt when opened.
  The tool does not open it: open it yourself first. For Excel also use `--in-place`, or save it as `.xlsx`.
- macOS asks to allow Accessibility (to see dialogs) and Automation (to control the app) for the terminal or
  MCP client. Office may ask for access to other files that a macro opens.

## Limits

- Code that does not compile, or a `MsgBox`, opens a dialog that you must answer.
  Until then the tool sends the app nothing, because Excel quits on a command sent during a dialog.
- An endless loop blocks the app.
- A macro acts on the active file. The target is checked just before it runs.
- Undo: Word and PowerPoint only. Files a macro writes or deletes cannot be undone.
- No breakpoints or stepping; use the VBA editor for those.

## To do

- Windows: live commands, possibly through COM. Ideas, none tried:
    - `Application.Run`, and `AddIns.Add` with `Installed` or `Loaded`, are available to COM directly,
      so Excel may not need the loader.
    - Calling COM from PowerShell would keep pyOpenVBA the only dependency; pywin32 would add one.
    - Check what macro prompts appear under COM without changing security settings, and find another way to
      see dialogs than the Mac's Accessibility.
- Windows and Linux: try `list`, `pull` and `install` on real machines.

## Test

```bash
PYTHONDONTWRITEBYTECODE=1 uv run --no-project --with-editable . --with pytest pytest -p no:cacheprovider
```

## Publish

`bash publish.sh --help`

## License

AGPL-3.0-or-later
