typescript-to-vba
v1.0.1
Published
Professional TypeScript to VBA transpiler for Excel/Office automation
Maintainers
Readme
TypeScript to VBA (TSTVBA)
Transpile modern TypeScript into VBA modules (.bas + .cls) for Excel/Office automation.
Installation
npm install -g typescript-to-vbaQuick start
tstvba --init
tstvba check -p .
tstvba -p .Local development usage (without global install):
npm install
npm run build
node dist/cli.js --init
node dist/cli.js check -p .
node dist/cli.js -p .Generated VBA output: dist/vba/MyProject.bas
Build now also generates a bundle manifest: dist/vba/tstvba-manifest.json.
Config model
tstvbaOptions are stored directly inside tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"strict": true,
"outDir": "dist/vba"
},
"tstvbaOptions": {
"entry": "example/main.ts",
"targetApplication": "Excel",
"moduleStyle": "StandardModule",
"vbaLibraryPath": "./lib/vbalib.bas",
"namespacePrefix": "TS_",
"emitSourceMaps": true,
"bundle": true,
"outputFileName": "MyProject.bas"
}
}Commands
node dist/cli.js -p .— build using project directory (tsconfig.jsonortsconfig.tstvba.json)node dist/cli.js --watch -p .— watch modenode dist/cli.js --init— createtsconfig.tstvba.json+lib/vbalib.basstubnode dist/cli.js check -p .— validate and print resolved config
Namespacing strategy (VBA global scope)
Template now uses Static Prefixing for exported function declarations:
- TS:
export function init()inmain.ts - VBA:
Public Sub TS_main_init()
Formula: <namespacePrefix><moduleName>_<functionName>
This avoids collisions between modules with identical function names.
Object bridge: Class Module Mirroring
Current template includes first implementation of TS class -> VBA .cls generation:
- each top-level
classgenerates a dedicated.clsfile in output directory; - generated class name uses namespacing pattern (
<namespacePrefix><moduleName>_<ClassName>); - constructor is mapped to
Public Sub Init(...); - class fields become
Private m_<field>+Property Get/Letaccessors; - methods are emitted as
Public Sub/Public Functionstubs.
Import note for .cls files
Class module files contain metadata header (VERSION/BEGIN/END + Attribute ...) and must be imported via VBE -> File -> Import File....
- If you copy/paste manually into a class code window, do not paste header lines above
Option Explicit. - Use generated
.clsfiles fromdist/vbadirectly for reliable class properties (MultiUse,VB_Name, etc.).
Error handling bridge: Try/Catch -> On Error
Template now includes first pass of exception mapping:
try/catch/finallyis emitted using unique label blocks (TS_CATCH_n,TS_FINALLY_n,TS_ENDTRY_n);throwis mapped toErr.Raise vbObjectError + 513, "TSTVBA", ...;- runtime auto-injects
error.stackhelpers (TS_PushError,TS_LastErrorMessage,TS_ClearError) when try/throw is detected.
Iteration bridge: For...Of
Template now includes first pass of advanced iteration mapping:
for...ofemits hybrid VBA loop strategy:- arrays:
For i = LBound(...) To UBound(...); - non-array collections:
For Each item In collection;
- arrays:
- runtime auto-injects
iterator.protocolhelperTS_HasArrayBoundswhenForOfStatementis detected.
Packaging strategy (Production bundle)
Current build pipeline includes packaging polish:
dist/vbais cleaned before each transpile run;- generated
.basfile gets metadata header:DO NOT EDIT - GENERATED CODE- compiler version
- build timestamp
- project path
- build writes
tstvba-manifest.jsonwith runtime features and all generated output files.
Project structure
src/cli.ts— CLI entrypointsrc/config.ts— tsconfig +tstvbaOptionsloadersrc/transpile.ts— transpilation pipeline templatesrc/emitter/vbaEmitter.ts— basic AST emitter skeleton
