diff --git a/.gitignore b/.gitignore index fd8f021..cc4ac38 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,4 @@ test_*.py debug_*.py # Demo file (optional - comment out if you want to track it) -# demo.xlsm +*.xlsm diff --git a/check_vba_access.py b/check_vba_access.py deleted file mode 100644 index 867cd85..0000000 --- a/check_vba_access.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -检查 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() diff --git a/create_demo.py b/create_demo.py deleted file mode 100644 index 5ed7d3d..0000000 --- a/create_demo.py +++ /dev/null @@ -1,83 +0,0 @@ -""" -创建演示用的 Excel 文件,包含测试用的 VBA 代码 -""" - -import xlwings as xw - -# VBA 测试代码 -VBA_TEST_CODE = """Option Explicit - -Sub TestErrorProcedure() - Dim x As Integer - Dim y As Integer - Dim result As Double - - x = 10 - y = 0 - result = x / y ' 这里会触发除以零错误 - - Debug.Print "Result: " & result -End Sub - -Sub TestTypeMismatch() - Dim x As String - Dim y As Integer - - x = "Hello" - y = x ' 这里会触发类型不匹配错误 - - Debug.Print "Y: " & y -End Sub - -Sub TestSubscriptError() - Dim arr(1 To 3) As Integer - Dim value As Integer - - arr(1) = 10 - arr(2) = 20 - arr(3) = 30 - value = arr(5) ' 这里会触发下标越界错误 - - Debug.Print "Value: " & value -End Sub - -Sub TestSuccessfulProcedure() - Dim x As Integer - Dim y As Integer - Dim sum As Integer - - x = 10 - y = 20 - sum = x + y - - Debug.Print "Sum: " & sum -End Sub -""" - -def create_demo_file(): - """创建演示用的 Excel 文件""" - app = xw.App(visible=True) - wb = app.books.add() - - # 添加 VBA 模块 - vba_project = wb.api.VBProject - module = vba_project.VBComponents.Add(1) # 1 = vbext_ct_StdModule - module.Name = "Module1" - module.CodeModule.AddFromString(VBA_TEST_CODE) - - # 保存文件 - file_path = r"D:\python\xlwings\demo.xlsm" - wb.save(file_path) - wb.close() - - print(f"演示文件已创建: {file_path}") - print("\n包含的测试过程:") - print(" - TestErrorProcedure: 除以零错误") - print(" - TestTypeMismatch: 类型不匹配错误") - print(" - TestSubscriptError: 下标越界错误") - print(" - TestSuccessfulProcedure: 成功执行的测试") - - app.quit() - -if __name__ == "__main__": - create_demo_file() diff --git a/demo.xlsm b/demo.xlsm deleted file mode 100644 index 687bd97..0000000 Binary files a/demo.xlsm and /dev/null differ