""" 检查 Excel VBA 项目访问设置 """ import winreg import os def check_vba_project_access(): """检查是否启用了对 VBA 工程对象模型的访问""" # Excel 版本的注册表路径 excel_versions = [ r"Software\Microsoft\Office\16.0\Excel\Security", r"Software\Microsoft\Office\15.0\Excel\Security", r"Software\Microsoft\Office\14.0\Excel\Security", ] print("Checking Excel VBA Project Access Settings...\n") found = False for version_path in excel_versions: try: key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, version_path) access_value, _ = winreg.QueryValueEx(key, "AccessVBOM") winreg.CloseKey(key) if access_value == 1: print(f"[OK] VBA Project Access is ENABLED in {version_path}") found = True else: print(f"[WARN] VBA Project Access is DISABLED in {version_path}") except FileNotFoundError: continue except Exception as e: print(f"[ERROR] Failed to check {version_path}: {e}") if not found: print("\n[INFO] VBA Project Access settings not found in registry.") print("You may need to enable it manually in Excel.") print("\n" + "="*60) print("How to Enable VBA Project Access in Excel:") print("="*60) print("1. Open Excel") print("2. Go to File > Options > Trust Center") print("3. Click 'Trust Center Settings...'") print("4. Go to 'Macro Settings'") print("5. Check the box: 'Trust access to the VBA project object model'") print("6. Click OK to save changes") print("7. Restart Excel") print("="*60) if __name__ == "__main__": check_vba_project_access()