VBA P-Code Disassembler

1.2.6 · active · verified Fri Apr 10

pcodedmp is a Python library and command-line tool for disassembling VBA p-code from Microsoft Office documents. It supports various Office formats (e.g., `.docm`, `.xlsm`, `.pptm`) and aims to provide detailed insight into embedded VBA macros for analysis. The current version is 1.2.6, with releases typically tied to bug fixes or feature additions for better p-code parsing.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pcodedmp` to disassemble VBA p-code from an Office document. It shows how to call the `dump_file` function and capture its output. Replace the placeholder `document_path` with an actual path to a `.docm`, `.xlsm`, or `.pptm` file containing VBA macros. The example gracefully handles `FileNotFoundError` and other exceptions.

import os
import io
import sys
from pcodedmp.pcodedmp import dump_file

# This library processes existing Office documents containing VBA macros.
# Replace 'path/to/your/document.docm' with the actual path to your target file.
# For example, create a simple .docm file with a basic macro (e.g., MsgBox 'Hello').
#
# If the file does not exist, the library will raise a FileNotFoundError.
# This example is designed to be runnable and show the expected output,
# whether it's successful disassembly or an error due to a missing file.
document_path = "path/to/your/document.docm" # Replace with a real path if you have one.

# Capture stdout to inspect the disassembly output without writing to console
original_stdout = sys.stdout
captured_output = io.StringIO()
sys.stdout = captured_output

try:
    print(f"Attempting to disassemble VBA p-code from: {document_path}")
    # The dump_file function writes output to sys.stdout by default.
    # You can also specify an output file: dump_file(document_path, output_file=open('output.txt', 'w'))
    dump_file(document_path)

    # Get the captured output
    output_lines = captured_output.getvalue().strip().split('\n')
    print("\n--- Disassembly Attempt Result ---")
    if output_lines and output_lines[0].startswith('VBA p-code disassembler'): # Check for actual content
        print("\n".join(output_lines[:10])) # Print first 10 lines of actual disassembly
        if len(output_lines) > 10:
            print("...")
        print(f"(Full output length: {len(output_lines)} lines)")
    else:
        # No relevant output from disassembly, likely an error message from the library itself
        print("No relevant disassembly output. See error messages below if any.")

except FileNotFoundError:
    print(f"\nERROR: Input file not found at '{document_path}'.")
    print("Please replace 'path/to/your/document.docm' with a valid path to an Office document containing VBA.")
except Exception as e:
    print(f"\nAn unexpected error occurred: {e}")
finally:
    sys.stdout = original_stdout # Restore original stdout

print("\nQuickstart finished. Check the 'Disassembly Attempt Result' above.")

view raw JSON →