Run Excel Macro from Access macro in VBA

陌路散爱 提交于 2021-01-29 17:09:20

问题


I am trying to run an excel macro from access 2016. This has a lot of examples out their however they are all old. The exact error code I receive is run-time error '1004': cannot run the macro "TestScript()". The macro may not be available in this workbook or all macros may be displayed. I am only running something easy for the test. I have ensured that macros is enabled in excel. the excel code is as follows

    Public Sub TestScript()

       MsgBox "IT WORKED"


    End Sub

Real simple for the test. Access is opening the excel spreadsheet however it stops there with an error code.
My Access code is very simple and is below. I have also noted where the code stops. While I am new at VBA I have done a lot of research in this. I am testing as simple code as I could figure out. Any help would be welcomed.


Option Compare Database


Function runExcelmacro()

    Dim XL As Object

    Set XL = CreateObject("Excel.Application")

    With XL

    'Turn Off warnings
    .Visible = False
    .displayalerts = False
    'WorkBook path such as "C:\Computer\WorkBook.xlsx"


    .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
    'Run the Macro in excel getworkbook

    .Run TestScript 'Code stops here!

    'Close Workbook

    .ActiveWorkbook.Close (True)
    .Quite
    End With
    Set XL = Nothing

End Function


Public Sub runMacroSub()
    Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub

回答1:


I guess OP did not put the code of Testscript in an extra module. Instead the code was put into the class module of the workbook or a worksheet. In the latter case you have to add the workbook or worksheet name in front of the name of the script.

Either it is `

.Run "ThisWorkbook.TestScript"

or in case it is in Sheet1 (codename of the sheet!)

.Run "Sheet1.TestScript"

Do not forget the quotation marks!

PS The OP's code above is working if you put testscript into a module and add quotation marks.

.Run "TestScript"

Here is a description how to create a module and add code




回答2:


Here try this:

Public Sub TestScript()
   MsgBox "IT WORKED"
End Sub

Option Compare Database

Function runExcelmacro()
    Dim XL As Object, wb as object
    Set XL = CreateObject("Excel.Application")
    With XL
        .Visible = False
        .displayalerts = False
        set wb = .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
        wb.Run TestScript 'Code stops here!
        .ActiveWorkbook.Close (True)
        .Quite
    End With
    Set XL = Nothing

End Function

Public Sub runMacroSub()
    Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub

Here i declared wb as an object and then set wb = to the workbook.

This is of course assumin your test script is in the actual workbook being opened. Would be real funny if that wasnt a thing lol



来源:https://stackoverflow.com/questions/59879272/run-excel-macro-from-access-macro-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!