我有一个 Python 程序(如 block 1 所示),用于打开 Excel 文件并调用宏。
Excel 宏(如 block 2 所示)打开特定文件夹中的所有 Excel 文件。
Python 程序打开 Excel 文件并调用宏,我已经使用不同的文件对其进行了测试,但是当我尝试此 Excel 文件时,它会打开,但宏不会运行。
这是 Python
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open("K:\\updatebloomberg.xlsm")
xl.Visible = True
wb.Application.Run("'updatebloomberg.xlsm'!Module1.runfiles()")
wb.Close(True)
这是 VBA
Sub runfiles()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim ws As Worksheet
Dim c As Range
myPath = "K:\Bloomberg Data\"
myExtension = "*.xlsx"
myFile = Dir(myPath & myExtension)
Do While myFile <> ""
Set wb = Workbooks.Open(Filename:=myPath & myFile)
myFile = Dir
Loop
Application.OnTime (Now + TimeValue("00:00:02")), "refreshSheet"
End Sub
最佳答案
你到底为什么要这么做???
import glob
for fnam in glob.glob("K:\Bloomberg Data\*.xlsx"):
os.startfile(fname)
如果这不起作用,您是否检查过您的网络驱动器((我假设是网络驱动器)在文件资源管理器中查看时上面没有 X)
关于python - 使用Python调用Excel宏打开文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39646877/