简体   繁体   English

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

[英]Calling VBA macro from .vbs file throws 800A03EC error

I'm trying to run a VBA macro through .VBS file(File name: Check_final.vbs). 我正在尝试通过.VBS文件(文件名:Check_final.vbs)运行VBA宏。 Here is the code 这是代码

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. 当我运行此.vbs文件时,出现带有只读和通知选项的弹出窗口'Changed_chk.xlsm已被锁定以进行编辑'。 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. 如果我通过“只读”或“通知”选项确认它,则以Check_final(即该.vbs文件的文件名)的名称打开一个excel工作表,并且上述代码显示为写在该excel文件中。 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/)] . 然后我收到一个Windows脚本主机错误(代码:800A03AC),说该宏可能不可用或所有宏都被禁用。(尽管我已按此处所述启用了该宏。[ http://www.addictivetips.com/windows-tips / enable-all-macros-in-excel-2010 /)] Any help on this is much appreciated. 任何帮助对此表示感谢。 Thanks in advance. 提前致谢。

You open your vbs-file instead of your excel-file... Also make sure that your function/sub is public. 您打开您的vbs文件而不是excel文件...。此外,请确保您的函数/子文件是公共的。 In the example below, I created a Public Sub Check in the module "YourModuleName", which I call from the vbs-file. 在下面的示例中,我在模块“ YourModuleName”中创建了一个Public Sub Check ,该模块从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

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 您可能希望看到

I think there may be something wrong with calling the run_macro statement. 我认为调用run_macro语句可能有问题。 From the test i created in excel VBA there is an error if i try to call the sub outside of another sub 根据我在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_macro line altogether? 或完全删除run_macro行?

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

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