I'm trying to figure out why my Word 2010 VBA will not run an Excel macro from within a Word macro.
I can successfully do the reverse and run a Word macro from an Excel macro (and pass variables into that Word macro) but get an error (shown below) when trying to do it the other way around, and don't know why.
It's probably something simple but it's eluding me...thanks for any help!
'This Excel macro WORKED to run a Word Macro and pass variables into it:
'***********************************************
Sub RunTestGetVarFromXL()
'This worked to run the Word TestGetVarFromXL macro.
'~~> Establish an Word application object
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wrdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
'Test assigning variables:
CourseStartRow = "44"
CourseEndRow = "54"
CourseTitle = "MS Word 2016"
'Using Call command to run this word macro and pass these variables into it:
Call wrdApp.Run("TestGetVarFromXL", CourseStartRow, CourseEndRow, CourseTitle) '
End Sub
'********************************************************
----------------------------------------------------------------------------
'This Word macro successfully received variable data from the Excel macro that called it:
'*****************************************************
Sub TestGetVarFromXL(CourseStartRow, CourseEndRow, CourseTitle)
'This Worked and received these variable data from Excel macro: RunTestGetVarFromXL
MsgBox ("From Word TestGetVarFromXL macro:" & Chr(13) & Chr(13) & _
"CourseStartRow = " & CourseStartRow & Chr(13) & Chr(13) & _
"CourseEndRow = " & CourseEndRow & Chr(13) & Chr(13) & _
"CourseTitle = " & CourseTitle)
End Sub
'***************************************************
===================================================================
=======================================================================
*** These two macros below did NOT Work**********************
They are similar to the above macros but changed in order to run an Excel macro from Word.
When running the Word macro RunTestGetVarFromWord, it gives the error message:
"Run-time error '1004': Cannot run the macro 'RunTestGetVarFromWord". The macro may not be available in this workbook or all macros may be disabled".
The Word macro meant to call Excel macro: didn't work, gave error:
'***************************
Sub RunTestGetVarFromWord()
'This did NOT work, currently getting error message:
'"Run-time error '1004': Cannot run the macro 'TestGetVarFromWord". The macro may
'not be available in this workbook or all macros may be disabled"
'~~> Establish an Excel application object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
'Test assigning variables to pass:
CourseTitle = "MS Word 2016"
CourseStartRow = "44"
CourseEndRow = "54"
'Run this Excel macro and pass these variables into it:
Call xlApp.Run("TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '
End Sub
'*********************************
Excel macro to receive variables from the Word macro that calls it: never ran.
'**************************************************
Sub TestGetVarFromWord(CourseStartRow, CourseEndRow, CourseTitle)
'This did NOT get called successfully from the Word macro: RunTestGetVarFromWord.
MsgBox ("From Excel TestGetVarFromWord macro:" & Chr(13) & Chr(13) & _
"CourseStartRow = " & CourseStartRow & Chr(13) & Chr(13) & _
"CourseEndRow = " & CourseEndRow & Chr(13) & Chr(13) & _
"CourseTitle = " & CourseTitle)
End Sub
'*****************************
I can successfully do the reverse and run a Word macro from an Excel macro (and pass variables into that Word macro) but get an error (shown below) when trying to do it the other way around, and don't know why.
It's probably something simple but it's eluding me...thanks for any help!
'This Excel macro WORKED to run a Word Macro and pass variables into it:
'***********************************************
Sub RunTestGetVarFromXL()
'This worked to run the Word TestGetVarFromXL macro.
'~~> Establish an Word application object
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wrdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
'Test assigning variables:
CourseStartRow = "44"
CourseEndRow = "54"
CourseTitle = "MS Word 2016"
'Using Call command to run this word macro and pass these variables into it:
Call wrdApp.Run("TestGetVarFromXL", CourseStartRow, CourseEndRow, CourseTitle) '
End Sub
'********************************************************
----------------------------------------------------------------------------
'This Word macro successfully received variable data from the Excel macro that called it:
'*****************************************************
Sub TestGetVarFromXL(CourseStartRow, CourseEndRow, CourseTitle)
'This Worked and received these variable data from Excel macro: RunTestGetVarFromXL
MsgBox ("From Word TestGetVarFromXL macro:" & Chr(13) & Chr(13) & _
"CourseStartRow = " & CourseStartRow & Chr(13) & Chr(13) & _
"CourseEndRow = " & CourseEndRow & Chr(13) & Chr(13) & _
"CourseTitle = " & CourseTitle)
End Sub
'***************************************************
===================================================================
=======================================================================
*** These two macros below did NOT Work**********************
They are similar to the above macros but changed in order to run an Excel macro from Word.
When running the Word macro RunTestGetVarFromWord, it gives the error message:
"Run-time error '1004': Cannot run the macro 'RunTestGetVarFromWord". The macro may not be available in this workbook or all macros may be disabled".
The Word macro meant to call Excel macro: didn't work, gave error:
'***************************
Sub RunTestGetVarFromWord()
'This did NOT work, currently getting error message:
'"Run-time error '1004': Cannot run the macro 'TestGetVarFromWord". The macro may
'not be available in this workbook or all macros may be disabled"
'~~> Establish an Excel application object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
'Test assigning variables to pass:
CourseTitle = "MS Word 2016"
CourseStartRow = "44"
CourseEndRow = "54"
'Run this Excel macro and pass these variables into it:
Call xlApp.Run("TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '
End Sub
'*********************************
Excel macro to receive variables from the Word macro that calls it: never ran.
'**************************************************
Sub TestGetVarFromWord(CourseStartRow, CourseEndRow, CourseTitle)
'This did NOT get called successfully from the Word macro: RunTestGetVarFromWord.
MsgBox ("From Excel TestGetVarFromWord macro:" & Chr(13) & Chr(13) & _
"CourseStartRow = " & CourseStartRow & Chr(13) & Chr(13) & _
"CourseEndRow = " & CourseEndRow & Chr(13) & Chr(13) & _
"CourseTitle = " & CourseTitle)
End Sub
'*****************************