Documentation
¶
Overview ¶
Package vba reads the source code of a VBA project: the modules of macros an Office file carries in a storage of its compound file (a presentation, in a compound file of its own within its stream). It is written from the format's published description, [MS-OVBA]. The code is read as text. Nothing in it is run.
A project keeps each module's source compressed, after a cache of compiled code that is not read. Its "dir" stream, compressed the same way, lists the modules, the stream each is in, and where in the stream the source starts.
The reader trusts nothing in the file: a module's source is read only up to a limit on what all of them decompress to, and anything out of place ends the module where it is, with what was read kept.
Index ¶
Constants ¶
const MaxStream = 1 << 28
MaxStream bounds the size of a stream of a project that is read: 256 MiB. A larger one is refused with ErrTooLarge.
Variables ¶
var ErrNotProject = errors.New("vba: no VBA project")
ErrNotProject says the compound file holds no VBA project.
var ErrTooLarge = errors.New("vba: a stream of the project is larger than 256 MiB")
ErrTooLarge says a stream of the project is larger than is read.
Functions ¶
Types ¶
type Module ¶
type Module struct {
// Name is the module's name ("Module1", "ThisWorkbook").
Name string
// Code is its source, lines ended by line feeds.
Code string
}
Module is one module of a project.
type Project ¶
type Project struct {
Modules []Module
// CodePage is the code page the project's text is written in.
CodePage int
// Undecoded counts characters of text that the code page does not
// define, or bytes of a code page not known: each became U+FFFD.
Undecoded int
// Cut says the limit was reached: a module is cut off, or later ones
// are left out.
Cut bool
// Damaged says some of the project could not be read: what was read
// up to the damage is kept.
Damaged bool
// FileDamaged is the compound file's own Damaged: set, to what was
// wrong, when the file the project is in was read in spite of damage
// to its structure. It is how a caller of ReadBytes learns of it.
FileDamaged string
// Unlisted counts the modules past maxModules, which are not read.
Unlisted int
}
Project is what Read finds in a project.
func Read ¶
Read reads the project in the storage of f that path names: the storage that holds the VBA storage, which is Macros in a Word file, _VBA_PROJECT_CUR in a workbook and the root of the file a presentation keeps its project in. limit bounds the source it gives, in bytes, all modules together.
func ReadBytes ¶
ReadBytes reads the project of a vbaProject.bin, the compound file an Office Open XML file keeps its macros in: the VBA storage is at its root. limit is Read's.
When the compound file opens but its project cannot be read, the error comes with a Project that holds only FileDamaged, so that a caller learns of damage to the file either way. When the file does not open, the Project is nil.