Calling VBA macro from .vbs file throws 800A03EC error

前端 未结 3 2051
失恋的感觉
失恋的感觉 2021-01-28 12:39

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

Option Explicit

run_macro
         


        
相关标签:
3条回答
  • 2021-01-28 13:19

    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 Check in the module "YourModuleName", which I call from the vbs-file.

    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
    
    0 讨论(0)
  • 2021-01-28 13:25

    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

    0 讨论(0)
  • 2021-01-28 13:26

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

    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?

    0 讨论(0)
提交回复
热议问题