I am using Excel 2010. I am opening test.xls file from VBS (VB script) using the below code:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("E:\test\test.xls")
objExcel.Application.Visible = True
the workbook test.xls opens fine, but the workbook_open macro that I put inside Thisworkbook Module:
Private Sub Workbook_Open()
MsgBox "Hello"
End Sub
doesn't run.
It works when I open test.xls directly, but not through VB Script.
I have set Developer > Macro Security > Macro Settings to Enable all Macros, but this still doesn't help.
Recently I faced the same issue. Here is what I did.
Save your file as .xlsm(marco-enabled excel).
Instead of writing your code in worksheet, create new module and copy your VBA code in a function or sub with name like 'Excel_Macro'.
Include the following two line in your VBS code (from where you are opening excel).
objExcel.Run("Excel_Macro")
This worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.