简体   繁体   中英

workbook_open not working when starting excel 2010 workbook from vbs

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.

  1. Save your file as .xlsm(marco-enabled excel).

  2. 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'.

  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM