从Access VBA运行Excel函数

时间:2014-11-18 14:28:56

标签: excel excel-vba access-vba vba

我有一个excel命令按钮,它只是将一张纸的内容复制到另一张。我试图从访问内部运行此命令。我没有收到错误,但是当我从访问中运行代码时,信息不会复制到excel中。代码如下。有什么想法吗?

'

Private Sub Command92_Click()
 Dim x2 As Object
 Dim GetDBPath As String
 GetDBPath = CurrentProject.Path & "\" & "Reports.xlsm"

Set x2 = CreateObject("Excel.Application")

x2.Workbooks.Open (GetDBPath)

x2.Visible = True

x2.Run CommandButton1_Click
x2.ActiveWorkbook.Close (True)

x2.Quit

Set x2 = Nothing


End Sub'

1 个答案:

答案 0 :(得分:1)

Run以String的形式获取要运行的例程的名称。您还需要在调用中包含工作表的代码名称:

x2.AutomationSecurity = msoAutomationSecurityLow
x2.Run "Sheet1.CommandButton1_Click"

例如。