从存储在Word中的宏运行要在该工作簿中执行的宏

时间:2014-03-07 08:46:59

标签: excel vba excel-vba

您有一个我想要执行以下操作的Word文档中的宏(我们称之为宏A):

  1. 打开一个已启用宏的工作簿,其中包含一个宏(宏B)
  2. 运行宏B。
  3. 关闭并将宏启用工作簿另存为.xls。
  4. 除了第2部分 - 运行宏之外,我已经成功地完成了所有工作。请帮忙!!!

    这是我到目前为止所做的:

    Public Sub Monthly_Commission_Extract()
    On Error GoTo ErrHandler:
    Dim oExcel As Excel.Application
    Dim oWB As Excel.Workbook
    Dim strFileName As String
    Dim SaveAs As String
    strFileName = "\\stnlinasshd01\P403759\Hiport Admin\Month End\Monthly Commission       Extract\MONTHLY COMMISSION MASTER"
    SaveAs1 = "\\stnlinasshd01\P403759\Hiport Admin\Month End\Monthly Commission Extract\2nd Save"
    
    Set oExcel = New Excel.Application
    Workbooks.Open FileName:="\\stnlinasshd01\P403759\Hiport Admin\Month End\Monthly    Commission Extract\MONTHLY COMMISSION MASTER.xlsm"
    oExcel.Visible = True
    
    Application.Run "MONTHLY COMMISSION MASTER.xls!Retrieve_Monthly_Commission_Data()"
    
    ActiveWorkbook.Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs SaveAs1, xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Application.DisplayAlerts = True
    
    ActiveWorkbook.Close
    DoEvents
    
    
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, vbCritical, "HELP ME"
    
    End Sub
    

    代码在此行失败:

    Application.Run "MONTHLY COMMISSION MASTER.xls!Retrieve_Monthly_Commission_Data()"
    

1 个答案:

答案 0 :(得分:1)

试试这个:

oExcel.Run "'MONTHLY COMMISSION MASTER.xlsm'!Retrieve_Monthly_Commission_Data"

或者更好的是,像这样使用Set

Dim oWB As Excel.Workbook
Set oWB = oExcel.Workbooks.Open(Filename:="\\stnlinasshd01\P403759\Hiport Admin\Month End\Monthly    Commission Extract\MONTHLY COMMISSION MASTER.xlsm")
oExcel.Visible = True

oExcel.Run "'" & oWB.Name & "'!Retrieve_Monthly_Commission_Data"