pub struct VbaModule {
pub name: String,
pub kind: VbaModuleKind,
pub source: String,
pub bound_sheet_id: Option<u64>,
pub prefix_bytes: Vec<u8>,
pub module_cookie: u16,
pub cached_compressed_source: Option<Vec<u8>>,
}Expand description
A single VBA module’s editable content plus the opaque bytes needed to keep Excel happy on export.
Fields§
§name: StringVB_Name – must satisfy validate_vba_module_name.
kind: VbaModuleKindWhat kind of module this is, and so how it binds to the workbook.
source: StringPlain VBA source text (no compression, no Attribute-line management
beyond what the caller writes – callers are expected to include the
Attribute VB_Name = "..." line themselves, matching how real
Excel-authored module streams are shaped).
bound_sheet_id: Option<u64>Required iff kind == Document: the sheet this module’s code
belongs to (or None/ignored for ThisWorkbook, which isn’t tied to
a specific sheet). Kept as a stable id (not a name) so sheet renames
don’t silently orphan the binding – deliberately NOT cascaded the
other direction (renaming this module does not rename the sheet, and
vice versa; Excel allows the two names to diverge).
prefix_bytes: Vec<u8>Opaque bytes forming the pre-TextOffset “p-code prefix” of this
module’s stream. Never reparsed or validated by this codebase –
proven (via the POC) that its content doesn’t need to correspond
to this module’s actual source, only its presence matters, as long
as it’s shaped the way real Excel’s module loader expects (a
naively zero-filled placeholder of the same length is NOT enough).
For an imported module these are the real bytes read back from the
original file; for a module created in this codebase they’re
vba_synth::synthetic_module_prefix()’s from-scratch, self-consistent
zero-procedure cache – see that module’s doc comment.
The module stream’s MODULECOOKIE record (0x002C) value. MS-OVBA
documents this as implementation-specific and ignorable on read, but
this codebase used to blindly overwrite every module’s (including
untouched, imported ones’) cookie with a hardcoded 0xFFFF on every
export – discovered while investigating why every workbook this
codebase produces failed has vb project in real Excel, by diffing
a re-exported real donor project’s dir stream against the
original’s record-by-record and finding this was the one place real
data was being discarded and replaced rather than round-tripped
verbatim. Preserved here instead so an imported module’s original
value survives re-export.
cached_compressed_source: Option<Vec<u8>>This module stream’s already-compressed source, as read back
verbatim from an imported file – None for a module created fresh
in this session (nothing to cache yet). set_vba_module_source
clears this whenever source is replaced. Export reuses the cached
bytes instead of recompressing source from scratch for every
module untouched by the CRUD operation that triggered the save.
Implementations§
Source§impl VbaModule
impl VbaModule
Sourcepub fn check_syntax(&self) -> Result<ModuleSyntax, Error>
pub fn check_syntax(&self) -> Result<ModuleSyntax, Error>
Checks this module’s source, naming it in any error.
The name matters more than it looks: a workbook can hold many modules
and visi macro check reports on all of them, so an error that does
not say which one it came from is close to useless.
A VbaModule does not know its project, so unlike the free
check_syntax this cannot conclude anything from a name it
fails to resolve – a sibling module may well declare it. Reach for
VbaProject::check_modules when the project is available; it is
strictly the better check.