Python 3.9调用Access宏报错:Visible属性引用无效问题排查求助
Let me walk through this issue with you—this error usually pops up when the Access COM object isn't fully initialized or configured properly, especially when switching from manual execution to task scheduler.
First, here's the original code that was working until the task scheduler setup:
import win32com.client from win32com.client import Dispatch strDbName = 'S:\Public\Maintenance\MaintPMprototypev1_4.accdb' objAccess = Dispatch("Access.Application") objAccess.Visible = False objAccess.OpenCurrentDatabase(strDbName) objAccess.DoCmd.RunMacro('autoexport') objAccess.Application.CloseCurrentDatabase() objAccess.Application.Quit()
And the error we're hitting:
Traceback (most recent call last):
File "C:\Users\charlie.zigler\Desktop\autoexport.py", line 10, in
objAccess.Visible = False
File "C:\Users\charlie.zigler\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 592, in setattr
self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, 'You entered an expression that has an invalid reference to the property Visible.', 'dao360.chm', 2015567, -2146825833), None)
Here are actionable fixes to try:
Adjust when you set the
Visibleproperty
The core issue here is likely that you're trying to setVisiblebefore the Access instance has a database context loaded. Move that line after opening the database—this ensures the Access object is fully initialized and the property is accessible:import win32com.client from win32com.client import Dispatch strDbName = 'S:\Public\Maintenance\MaintPMprototypev1_4.accdb' objAccess = Dispatch("Access.Application") objAccess.OpenCurrentDatabase(strDbName) objAccess.Visible = False # Moved to after database load objAccess.DoCmd.RunMacro('autoexport') objAccess.CloseCurrentDatabase() # Simplified from objAccess.Application.CloseCurrentDatabase() objAccess.Quit() # Same here—direct call is more reliableVerify Task Scheduler permissions
Task Scheduler runs with different permissions than your interactive user session:- Make sure the user account assigned to the task has read/write access to the
S:\Public\Maintenance\network path and the Access database file. - If you selected "Run whether user is logged on or not", ensure the account has permissions to interact with COM objects (avoid using the System account unless it can access your shared
S:\drive). - Check that the task's "Start in" directory is set to a location the account can access (like your desktop where the script lives).
- Make sure the user account assigned to the task has read/write access to the
Add a small delay for COM object initialization
Sometimes the Access COM object takes a split second to fully spin up. Adding a short sleep can prevent race conditions:import win32com.client import time from win32com.client import Dispatch strDbName = 'S:\Public\Maintenance\MaintPMprototypev1_4.accdb' objAccess = Dispatch("Access.Application") time.sleep(1) # Give Access time to initialize objAccess.OpenCurrentDatabase(strDbName) objAccess.Visible = False objAccess.DoCmd.RunMacro('autoexport') objAccess.CloseCurrentDatabase() objAccess.Quit()Check Access Trust Settings
If your database is marked as untrusted by Access, it can block property access in non-interactive mode:- Manually open
MaintPMprototypev1_4.accdbonce to confirm there's no security warning. - Add the
S:\Public\Maintenance\folder to Access's Trusted Locations (File > Options > Trust Center > Trust Center Settings > Trusted Locations).
- Manually open
内容的提问来源于stack exchange,提问作者Charles

