vba 从 Access 打开 Excel 工作簿宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12043718/
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-11 17:23:48  来源:igfitidea点击:

Opening an Excel workbook macro from Access

vbaexcel-vbams-access-2007access-vbaexcel

提问by ApplePie

First of all, I have searched and read similar threads and I have tried the solutions that were proposed and solved the other people's problem (mostly using single quotes around the workbook name) but it hasn't worked in my case.

首先,我搜索并阅读了类似的线程,并尝试了提出的解决方案并解决了其他人的问题(主要是在工作簿名称周围使用单引号),但在我的情况下没有用。

What I want to do is very simple: I want to call an Excel workbook from Access. The workbook is already open (using VBA) so this is not an issue of the workbook being closed. Here is how I attempt to run the macro:

我想做的很简单:我想从 Access 调用 Excel 工作簿。工作簿已经打开(使用 VBA),所以这不是工作簿被关闭的问题。这是我尝试运行宏的方式:

Application.Run "'" & xlWb.Path & "\" & xlWb.Name & "'" & "!mainParcourirTrouverItem"

Results in:

结果是:

'I:\47491\...\Trouver items global.xlsm'!mainParcourirTrouverItem

'I:\47491\...\Trouver items global.xlsm'!mainParcourirTrouverItem

I have tried a few variations like not using the complete path (only file name), not using single quotes, adding parentheses at the end of the sub's name...

我尝试了一些变体,例如不使用完整路径(仅文件名)、不使用单引号、在子名称末尾添加括号...

Also, I have double-checked that the spelling of the sub name is actually correct. I really don't see it :|

另外,我已经仔细检查了子名称的拼写是否正确。我真的没看到:|

Oh and just for the sake of giving context, here is the complete sub surrounding this code:

哦,只是为了提供上下文,这里是围绕此代码的完整子:

Private Sub Commande10_Click()
    Dim xlApp As Excel.Application
    Dim xlWb As Excel.Workbook

    Set xlApp = New Excel.Application

    With xlApp
        .Visible = False
        Set xlWb = .Workbooks.Open(FichierImportExcel, ReadOnly:=True)
    End With

    Application.Run "'" & xlWb.Path & "\" & xlWb.Name & "'" & "!mainParcourirTrouverItem"

'    Call importer_transitsrubriques_Click
End Sub

Many thanks.

非常感谢。

回答by Tim Williams

xlApp.Runnot Application.Run

xlApp.Run不是 Application.Run

Applicationwill refer to Access, not Excel.

Application将引用 Access,而不是 Excel。