INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can't get Word VBA to run an Excel macro

Can't get Word VBA to run an Excel macro

(OP)
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
'*****************************

RE: Can't get Word VBA to run an Excel macro

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")
    '
    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
    '
    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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Can't get Word VBA to run an Excel macro

(OP)
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

RE: Can't get Word VBA to run an Excel macro

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.

RE: Can't get Word VBA to run an Excel macro

CODE

'...
Call xlApp.Run("Personal.xlsb!TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '
'... 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Can't get Word VBA to run an Excel macro

(OP)
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

RE: Can't get Word VBA to run an Excel macro

Yes, plz start a new thread.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Can't get Word VBA to run an Excel macro

BTW, This could be as simple as doing this 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)
    
    CourseTitle = "This is from Excel"

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

...and doing this in your Word procedure...

CODE

'.....
    Call xlApp.Run("TestGetVarFromWord", CourseStartRow, CourseEndRow, CourseTitle) '

    MsgBox CourseTitle
'.... 
...so now you see that CourseTitle contains "This is from Excel"

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close