fix: address code review issues for Access support

- Fix import_vba_access() to actually save database via DoCmd.Save()
- Update stale module docstring in extract_vba.py
- Validate file extensions in get_file_type() instead of silently
  defaulting to excel
- Scan both Excel/ and Access/ directories in interactive mode

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-05-11 09:44:55 +08:00
parent ee1120c247
commit d156fdb2d8
2 changed files with 21 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
""" """
VBA代码提取工具 VBA代码提取工具
xlsm文件中提取模块和类模块代码分类保存到VBA文件夹 Excel(.xlsm)或Access(.accdb)文件中提取VBA代码分类保存到VBA文件夹
自动清理Attribute信息并生成元数据JSON文件 自动清理Attribute信息
""" """
import os import os
@@ -42,11 +42,16 @@ def get_file_type(file_path: Path) -> str:
Returns: Returns:
'access''excel' 'access''excel'
Raises:
ValueError: 不支持的文件扩展名
""" """
ext = file_path.suffix.lower() ext = file_path.suffix.lower()
if ext in ACCESS_EXTENSIONS: if ext in ACCESS_EXTENSIONS:
return 'access' return 'access'
if ext in EXCEL_EXTENSIONS:
return 'excel' return 'excel'
raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb")
class VBAExtractor: class VBAExtractor:
@@ -461,15 +466,14 @@ def main():
print() print()
else: else:
# 交互模式:查找支持的文件 # 交互模式:查找支持的文件
excel_dir = Path("Excel") all_files = []
if not excel_dir.exists(): for scan_dir in [Path("Excel"), Path("Access")]:
print("错误: 未找到Excel文件夹") if scan_dir.exists():
return for ext in ["*.xlsm", "*.accdb", "*.mdb"]:
all_files.extend(scan_dir.glob(ext))
# 同时扫描 Excel 和 Access 文件
all_files = list(excel_dir.glob("*.xlsm")) + list(excel_dir.glob("*.accdb")) + list(excel_dir.glob("*.mdb"))
if not all_files: if not all_files:
print("错误: Excel文件夹中没有.xlsm或.accdb文件") print("错误: 未找到.xlsm或.accdb文件请检查Excel/或Access/文件夹)")
return return
# 如果有多个文件,让用户选择 # 如果有多个文件,让用户选择

View File

@@ -50,11 +50,16 @@ def get_file_type(file_path: Path) -> str:
Returns: Returns:
'access''excel' 'access''excel'
Raises:
ValueError: 不支持的文件扩展名
""" """
ext = file_path.suffix.lower() ext = file_path.suffix.lower()
if ext in ACCESS_EXTENSIONS: if ext in ACCESS_EXTENSIONS:
return 'access' return 'access'
if ext in EXCEL_EXTENSIONS:
return 'excel' return 'excel'
raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb")
class VBAImporter: class VBAImporter:
@@ -411,8 +416,7 @@ class VBAImporter:
print("\n正在保存...") print("\n正在保存...")
try: try:
import time access.DoCmd.Save()
time.sleep(1) # 等待 VBE 完成
print("已保存更改。") print("已保存更改。")
except Exception as e: except Exception as e:
print(f"保存时出错: {e}") print(f"保存时出错: {e}")