从 .vbs 文件调用 VBA 宏会引发 800A03EC 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23360041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:55:27  来源:igfitidea点击:

Calling VBA macro from .vbs file throws 800A03EC error

vbaexcel-vbavbscriptexcel

提问by Code_Kid

I'm trying to run a VBA macro through .VBS file(File name: Check_final.vbs). Here is the code

我正在尝试通过 .VBS 文件(文件名:Check_final.vbs)运行 VBA 宏。这是代码

Option Explicit

run_macro

Sub run_macro()
    Dim xl1
    Dim sCurPath
    Dim xlBook
    Dim FolderFromPath
    Set xl1 = CreateObject("Excel.application")

    sCurPath =Wscript.ScriptFullName                                                                            
    Set xlBook = xl1.Workbooks.Open(sCurPath, 0, True)
    xl1.DisplayAlerts = False
    FolderFromPath = Left(sCurPath, InStrRev(sCurPath, "\"))
    xl1.Application.run FolderFromPath & "Changed_chk.xlsm!Check"

    Set xlBook = Nothing
End Sub

When I run this .vbs file I get this popup 'Changed_chk.xlsm is locked for editing' with Read only and notify options. If I acknowledge it with either Read only or notify option a excel sheet is opened in the name of Check_final (which is the file name of that .vbs file) and the above mentioned code is shown written in that excel file. Then I get a Windows script host error(code: 800A03AC) saying macro may not be available or all macro's are disabled.(Though I have enabled the macro as mentioned here.[http://www.addictivetips.com/windows-tips/enable-all-macros-in-excel-2010/)]. Any help on this is much appreciated. Thanks in advance.

当我运行这个 .vbs 文件时,我得到这个带有只读和通知选项的弹出窗口“Changed_chk.xlsm 被锁定以进行编辑”。如果我使用只读或通知选项确认它,则会以 Check_final 的名称(这是该 .vbs 文件的文件名)打开一个 Excel 工作表,并且上面提到的代码显示在该 Excel 文件中。然后我收到一个 Windows 脚本主机错误(代码:800A03AC),说宏可能不可用或所有宏都被禁用。(虽然我已经启用了这里提到的宏。[ http://www.addivetips.com/windows-tips /enable-all-macros-in-excel-2010/)]。非常感谢您对此的任何帮助。提前致谢。

回答by CommonGuy

You open your vbs-file instead of your excel-file... Also make sure that your function/sub is public. In the example below, I created a Public Sub Checkin the module "YourModuleName", which I call from the vbs-file.

你打开你的 vbs 文件而不是你的 excel 文件......还要确保你的函数/子是公开的。在下面的示例中,我Public Sub Check在模块“YourModuleName”中创建了一个,我从 vbs 文件调用它。

Option Explicit

run_macro

Sub run_macro()
    Dim xl1
    Dim xlBook
    Dim FolderFromPath
    Set xl1 = CreateObject("Excel.application")

    FolderFromPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "") 
    set xlBook = xl1.Workbooks.Open(FolderFromPath & "Changed_chk.xlsm")
    xl1.Application.run "'" & xlBook.Name & "'!YourModuleName.Check"
    xl1.Application.Quit
End Sub

回答by Siddharth Rout

Try this simple code (UNTESTED)

试试这个简单的代码(未经测试)

Dim oXlApp, oXLWb, sCurPath

Set oXlApp = CreateObject("Excel.application")

sCurPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set oXLWb = oXlApp.Workbooks.Open(sCurPath & "Changed_chk.xlsm")

oXlApp.DisplayAlerts = False

oXlApp.Run "Check"

'~~> Close the file here. Save or discard the changes as per your requirement 
'oXLWb.Close (True)
'oXLWb.Close (False)
oXLWb.Close
oXlApp.Quit

Set oXLWb = Nothing
Set oXlApp = Nothing

Also where is your macro? In a sheet or in a module? You may want to see THIS

还有你的宏在哪里?在工作表中还是在模块中?你可能想看看这个

回答by AWS-Cloud-Architect-Rob

I think there may be something wrong with calling the run_macrostatement. From the test i created in excel VBA there is an error if i try to call the sub outside of another sub

我认为调用run_macro声明可能有问题。从我在 excel VBA 中创建的测试中,如果我尝试在另一个子程序之外调用子程序,则会出现错误

Option Explicit
test

Sub test()
    MsgBox ("Test")
End Sub

I think you may want to

我想你可能想要

Option Explicit
Sub Start
    run_macro
End Sub

Sub run_macro()
    'code here
End Sub

or remove the run_macroline altogether?

run_macro完全删除该行?