While using ExecuteExcel4Macro
to run a Excel macro in Python, I always get the False result, here is the code executed:
import win32com.client
filename = r'E:\excel.xls'
xlApp = win32com.client.Dispatch('Excel.Application')
xlApp.visible = 1
xlBook = xlApp.Workbooks.Open(filename)
strPara = xlBook.Name + '!Macro1()'
res = xlApp.ExecuteExcel4Macro(strPara)
print res
xlBook.Close(SaveChanges=0)
and the output of "print res" statement is: False
After I search the usage of ExecuteExcel4Macro
on MSDN, I get the following information:
ExecuteExcel4Macro -- Runs a Microsoft Excel 4.0 macro function and then returns the result of the function. The return type depends on the function.
Then I get confused: since macro in Excel is always a "Sub procedure" and a "Sub procedure" in VBA has no return result, how can a Excel macro return a result? Then what does the False result in the above example stand for?
After that, I try ExecuteExcel4Macro
within Excel(2003) by coding not in Python but in VBA:
Sub RunMacro()
res = ExecuteExcel4Macro("excel.xls!Macro1()")
MsgBox CStr(res)
End Sub
Sub Macro1()
MsgBox "in Macro1"
End Sub
and the "res" string shown in MsgBox
is the same: False
1.Why is the return result of ExecuteExcel4Macro
always False?
2.What should I do if I want to run an Excel macro in Python and to get the exit status of the Excel macro function?
Updated at 2011.10.28:
Sub TEST()
res = Application.Run(MacroToRun)
MsgBox CStr(res)
End Sub
Function MacroToRun()
MacroToRun = True
End Function
After I run TEST Macro in Excel 2003, I get this:
A dialog with the information "Error 2015".