5.1 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
Auto_BOM is a VBA code extraction and management toolkit for Excel-based Bill of Materials (BOM) processing. The project provides Python tools to extract VBA code from .xlsm files, manage it externally, and import it back into Excel.
The VBA code implements a hierarchical BOM management system with:
- clsBOMManager: Main class managing BOM data structure and category relationships
- clsCategory: Represents material categories with hierarchical parent-child relationships
- clsMaterialItem: Represents individual materials with code, name, quantity, and selection conditions
Directory Structure
Auto_BOM/
├── Excel/ # Source Excel files (.xlsm) - gitignored
├── VBA/ # Extracted VBA code - gitignored
│ ├── Modules/ # Standard modules (.bas)
│ ├── ClassModules/ # Class modules (.cls)
│ ├── DocumentModules/# Sheet/workbook modules (.cls)
│ ├── Forms/ # User forms
│ └── vba_metadata.json # Module metadata for import
├── extract_vba.py # Extract VBA from Excel files
├── import_vba.py # Import VBA back to Excel files
├── main.py # Empty placeholder
└── requirements.txt # Python dependencies
Development Setup
# Create and activate virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
Dependencies:
pywin32>=306- Windows COM interface for Excel automation (Windows only)oletools>=0.60- Alternative VBA extraction without Excel dependency
Common Commands
Extract VBA Code
# Interactive extraction - will prompt for file and method
python extract_vba.py
Two extraction methods available:
- COM Interface (recommended) - Requires Microsoft Excel, more reliable
- olevba Library - No Excel required, uses oletools
For COM method, ensure Excel trusts VBA access:
- Excel > Options > Trust Center > Trust Center Settings
- Check "Trust access to the VBA project object model"
Import VBA Code
# Import from VBA/ directory back to Excel
python import_vba.py VBA/vba_metadata.json
VBA Code Architecture
BOM Data Model
The VBA system implements a hierarchical category-based material management:
-
Two-source loading pattern:
[平台配置清单]sheet: Contains all material info (code, name, quantity, condition)[领料配置]sheet: Defines categories and which materials require picking
-
Category hierarchy:
- Materials organized in parent-child category relationships
useParent=True: Pick assembled components from parent category (default)useParent=False: Pick individual parts from child categories (fallback when stock insufficient)
-
Data structures:
dictCategories: Dictionary for fast category lookup by namedictAllMaterials: Dictionary for fast material lookup by coderootCategories: Collection of top-level categories for tree traversal
Module Types
- Modules: Standard VBA modules (
.basfiles) - ClassModules: Class definitions (
.clsfiles) - clsBOMManager, clsCategory, clsMaterialItem - DocumentModules: Sheet and workbook code-behind (
.clsfiles) - Forms: UserForm definitions
Important Implementation Details
VBA Extraction (extract_vba.py)
- Cleans
Attributestatements from exported code for readability - Automatically categorizes modules by type (Standard/Class/Document/Form)
- Generates
vba_metadata.jsontracking source file, module names, types, and file mappings - Module type detection based on naming conventions (mod_=Standard, cls=Class, sheet=Document)
VBA Import (import_vba.py)
- Uses Windows COM to interact with Excel
- Critical fix for ClassModules: Reconstructs
VERSION 1.0 CLASSheader before import - Encoding handling: Uses GB18030 for temp files to prevent Chinese character corruption
- Path recognition logic handles relative/absolute paths in metadata
- Two import strategies:
- Modules/ClassModules: Remove and re-import via file
- DocumentModules/Forms: Update code in-place via string injection
Module Naming Convention
The code determines module type by naming prefix:
mod_*ormod*→ Standard Modulescls*orclass*→ Class Modulessheet*orthisworkbook→ Document Modules
VS Code Configuration
The .vscode/settings.json associates .cls files with Visual Basic syntax highlighting for better editing experience.
Platform Requirements
- Windows required for import functionality (COM interface)
- Microsoft Excel required for COM-based extraction/import
- Cross-platform extraction possible with oletools (no Excel needed)
Git Workflow
The .gitignore excludes:
- Virtual environment (
.venv/) - Build artifacts (
build/,dist/) - Project data (
Excel/,VBA/) - Claude temporary files (
.claude/,tmpclaude-*)
Only commit code changes, not extracted VBA or Excel files.