User forms
User forms
A form's design lives in forms/<name>.form.json (FormSpec), and the code for event handling and so on is written in RAS in forms/<name>.ras (spec §14.11). You can edit them in the designer of the VS Code extension and of the playground, and ras pack / ras sync write them into the workbook.
ras form new frmInput # creates forms/frmInput.form.json and forms/frmInput.ras
ras form pull # extracts the user forms in the workbook into forms/ (code is converted to RAS)
Code-behind
// forms/frmInput.ras — this is the form; controls are this.<name>
@on("btnOK.Click")
function ok() {
if (this.txtName.text == "") {
msgBox("Please enter a name");
return;
}
this.close(this.txtName.text);
}
@on("txtName.Exit")
function leave(cancel: MSForms.ReturnBoolean) {
cancel.value = this.txtName.text.length > 20;
}
- Events are received by functions marked with
@on("<control name>.<event>")(for the form itself,UserForm.Initializeand the like). The compiler generates event procedures such asPrivate Sub btnOK_Click(). - To attach the same handling to many controls, do it at run time in the
Initializehandler, withthis.controls.on("Change", (c) => { … })/this.btnOK.addEventListener("Click", ok)(this generates a class holdingWithEventsvariables; the handlers are detached when the form closes. Spec §12.2.3). - An
export functionbecomes a public member of the form, and other modules can use it asfrmInput.show()/frmInput.enteredName().
Arguments and return value
If you write function constructor(...), you can create an instance of the form with arguments, as in new frmInput("Yamada"). showModal() shows the form modally and returns the value passed to this.close(value) in the code-behind (for the × button or a close() without a value, it returns the default value of the type. Spec §14.11.9).
// src/ask.ras
export function askName(): string {
return new frmInput().showModal();
}
Design
- For an
Image, specify an image file in the project, as in"picture": { "path": "forms/assets/logo.png" }(BMP / JPEG / PNG / GIF / WebP / TIFF / ICO). The image is embedded in the workbook. BMP (24 / 32-bit uncompressed) and JPEG, which MS-Forms can store as they are, are embedded unchanged; anything else is converted to a 24-bit BMP when written (transparent areas are filled with the control'sbackColor. The original file is not modified). Images can be set only in the designer of the VS Code extension (not in the playground's designer). - Specify the items of a
ComboBox/ListBoxwith"list": ["A", "B"]and the initial selection with"selectedIndex": 0. Items are not stored as designer information, so code that sets the items when the form is loaded (RAS_FormInit) is generated and added to the code module. - You can also place a progress bar (
"type": "ProgressBar") and a file / folder picker ("type": "FilePicker") (Form controls). - For input validation, Input validation lets you show errors below the controls just by writing rules.
- Even when the Windows display scale (DPI) is 125%, 150% or the like, the inner size of the form is as designed (it is adjusted when the form is loaded so that the difference between the title bar and the frame does not clip controls or leave margins. Spec §14.11.10).
For dialogs built only in code, without a form design, see Dialogs.
Importing existing forms
Existing forms made in the VBE can be extracted into forms/ with ras form pull (design, code and images). The code is converted from VBA to RAS and written to forms/<name>.ras. Lines that could not be converted (GoTo, ReDim and so on) remain as // TODO(VBA): comments and are shown as warnings. Properties that FormSpec cannot express are also shown as warnings, and revert to their default values when written back from forms/. After extracting, replace the workbook's forms with the contents of forms/ using ras pack --overwrite.
A VBA code-behind forms/<name>.bas from earlier versions can still be used as it is if there is no .ras. Re-importing with ras form pull --force converts it to a .ras.
Applying to the workbook
ras pack / ras sync write the forms into the workbook (Syncing with and importing from Excel). Even while the workbook is open in Excel, ras sync (and ras watch, and the sync on save in VS Code) apply the forms. A form whose design changed is imported again; a form whose code alone changed has only its code rewritten.
Running
You can run a form directly from the CodeLens "▶ Run in Excel | Debug" at the top of forms/<name>.ras (in the designer, the buttons in the title bar). "Run in Excel" syncs to the workbook and then shows the form in Excel itself (Windows only); "Debug" shows the form in a VS Code panel and debugs the code-behind with breakpoints (Debugging). In the playground, a form shown during a run appears on top of the page. In a debug run, the form's appearance is an approximation in the browser, and key and mouse events do not occur (see spec §14.10.7 for what is supported).
The designer's operations (toolbox, properties, shortcuts) are described in the VS Code extension.