1
votes

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.

1
Works for me. You may want to try disabling Protected View, if it's enabled.Bond

1 Answers

0
votes

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.