vba

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 8 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var ErrNotProject = errors.New("vba: no VBA project")

ErrNotProject says the compound file holds no VBA project.

View Source
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

func Decompress

func Decompress(src []byte, limit int64) ([]byte, error)

Decompress decompresses a compressed container ([MS-OVBA] 2.4.1), giving at most limit bytes. What was decompressed before an error is returned with it; reaching the limit is an error too.

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

func Read(f *cfb.File, path []string, limit int64) (*Project, error)

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

func ReadBytes(data []byte, limit int64) (*Project, error)

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.

Directories

Path Synopsis
Package vbatest builds VBA projects for tests ([MS-OVBA]): compressed containers and the streams of a project, in a compound file.
Package vbatest builds VBA projects for tests ([MS-OVBA]): compressed containers and the streams of a project, in a compound file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL