'Looking for a complete Python script so that it runs my Macro?
I'm trying to create a Python script that opens MS Access and runs a Macro. I have the following script:
import win32api,time
from win32com.client import Dispatch
strDbName = 'Exit.mdb'
objAccess = Dispatch("Access.Application")
objAccess.Visible = False
objAccess.OpenCurrentDatabase(strDbName)
objDB = objAccess.CurrentDb()
objAccess.DoCmd.RunMacro('ImportDataFiles')
objAccess.Application.Quit()
When I run the above script, I get the following error:
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)
The macro is saved in a database file called: Sample_Database
The macro is called: ImportDataFiles The macro calls on a function which is written in vba. what it does is it imports files from a specific directory into the database. This is the full function:
Option Compare Database
Option Explicit
Public Function import_date_files()
Dim report_path As String, file_name As String
report_path = "C:\Users\Hamid\Documents\"
file_name = Dir(report_path & "*.csv", vbDirectory)
Do While file_name <> vbNullString
DoCmd.TransferText acImportDelim, , Trim(Replace(file_name, ".csv", "")), report_path & file_name, True
file_name = Dir
Loop
MsgBox "Data files imported.", vbInformation
End Function
I'd like someone to adjust the code above into one that works so that it runs the macro from a specific accdb database file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|