Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get Word VBA to run an Excel macro

Status
Not open for further replies.

rarkin

Technical User
Mar 10, 2014
7
US
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
'*****************************
 
Hi,

Your procedure, RunTestGetVarFromWord, opens no specific workbook nor references a specific workbook, the workbook that supposedly houses TestGetVarFromWord.

I got your Excel Macro to run using this code...
Code:
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"
    Dim xlApp, CourseTitle, CourseStartRow, CourseEndRow
'~~> Establish an Excel application object
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    '[b]
    If Err.Number <> 0 Then
        Set xlApp = CreateObject("Excel.Application")
        xlApp.workbooks.Open "\\SKIPSPC\Users\Skip\Documents\tt-word2excel.xlsm"
    Else
        xlApp.workbooks("tt-word2excel.xlsm").Activate
    End If
    '[/b]
    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

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Skip,
Thanks for the response! I tried your suggestions and made the changes as follows below, also noticing that I had no Dim statement for xlApp, but it still gives me the same error message for the Call command line. The excel macro: TestGetVarFromWord(CourseStartRow,CourseEndRow,CourseTitle) is located in Module1 of my VBAProject(PERSONAL.XLSB) project. So something on my setup is not referencing correctly differently from yours somehow.

Sub RunTestGetVarFromWord()

Dim xlApp, CourseTitle, CourseStartRow, CourseEndRow

'~~> ********** Establish an Excel application object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")

'Correction suggested by forum user who added these lines, said it needed to reference a
'specific workbook that supposedly houses TestGetVarFromWord.
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application")
'This line added to make it work:
xlApp.Workbooks.Open "C:\Users\Rarkin\Dropbox\OTACrsContent\Free\YouTube\YouTubeCrsBlank.xlsx"

'This Else statement added also:
Else
xlApp.Workbooks("YouTubeCrsBlank.xlsx").Activate
End If
Err.Clear
On Error GoTo 0
'***********************************************
'Test assigning variables:
CourseTitle = "MS Word 2016"
CourseStartRow = "44"
CourseEndRow = "54"

'Run the word macro and pass these variables into it:
Call xlApp.Run("TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '

End Sub
 
rarkin,
Help Skip - and everybody else - and use TGML tags to format your code, please.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Code:
'...
Call xlApp.Run("Personal.xlsb!TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '
'...

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Apologies for the messy code. Skip, you're a rock star! Problem solved---your suggestion to add the VBAProject name "Personal.xlsb!" to the called macro name solved it and was the only change needed.

I do have another question somewhat related to this, and will start a different thread for that--but I might as well share this with you in case you have the quick answer:
If the called excel macro TestGetVarFromWord assigned a value to some other variable, can that variable value then be passed back again into the word RunTestGetVarFromWord procedure?

Here's my final code (note the CourseStartRow, CourseEndRow, CourseTitle variables had been already declared publicly):

Sub RunTestGetVarFromWord()
'
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
'
CourseTitle = "MS Word 2016"
CourseStartRow = "44"
CourseEndRow = "54"
'
Call xlApp.Run("Personal.xlsb!TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle)
'
End Sub
 
Yes, plz start a new thread.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
BTW, This could be as simple as doing [highlight #FCE94F]this[/highlight] in the Excel procedure...
Code:
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)
    
    [highlight #FCE94F]CourseTitle = "This is from Excel"[/highlight]

End Sub
...or add another argument,...

...and doing [highlight #FCE94F]this[/highlight] in your Word procedure...
Code:
'.....
    Call xlApp.Run("TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '

   [highlight #FCE94F] MsgBox CourseTitle[/highlight]
'....
...so now you see that CourseTitle contains "This is from Excel"

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top