简体   繁体   English

从VBS启动Excel 2010工作簿时workbook_open无法正常工作

[英]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: 我正在使用Excel2010。我正在使用以下代码从VBS(VB脚本)打开test.xls文件:

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: 工作簿test.xls可以正常打开,但是我在Thisworkbook Module中放置的workbook_open宏:

Private Sub Workbook_Open()
MsgBox "Hello"
End Sub

doesn't run. 不运行。

It works when I open test.xls directly, but not through VB Script. 当我直接打开test.xls而不是通过VB脚本时,它可以工作。

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). 将文件另存为.xlsm(已启用marco的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'. 无需在工作表中编写代码,而是创建新模块,然后将VBA代码复制到名称为“ Excel_Macro”的函数或子程序中。

  3. Include the following two line in your VBS code (from where you are opening excel). 在VBS代码中包含以下两行(从此处打开excel)。

objExcel.Run("Excel_Macro") objExcel.Run( “Excel_Macro”)

This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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