Excel Macro call Word Macro with Parameters
Bernie,
I love the idea! I understand you to mean that I take the code in the Word
macro and place it within the loop of the Excel macro, but surround it with a
"with" structure.
But, I am unsure of how to do that. Can you give me an example?
"Bernie Deitrick" wrote:
Bill,
Why use the Word macro at all? You can do anything to Word from Excel that
you can do within Word, so that way, you would never pass control to Word,
and timing would never be an issue. Usually, you can just put the Word
Object as the object qualifier in the code, and it will run as well from
Excel as from Word.
Just a thought,
Bernie
MS Excel MVP
"Bill Sturdevant" wrote in
message ...
Thanks, Bernie, I am successfully calling the Word macro and passing it
parameters now, but... let me explain a bit more about what I am trying
to
do and ask some further questions.
I have a macro in an Excel Workbook. It is looking at a range of rows and
where conditions are right, it needs to call a macro in Word passing it
some
data from the chosen row.
The word macro creates a paragraph using the first parameter passed, then
using the rest of the parameters, builds 3 Excel Chart Objects, sizing
them
so they fit on one line, then puts two blank lines after the 3rd Chart.
Then, control must pass back to the Excel macro which loops until it finds
another acceptable row. This loop continues until the end of the range of
rows is met.
At the end of the first pass through the Word macro, control gets returned
back to Excel. If I stop the macros and look at the Word document, the
charts are there, but the values are not what was passed in. If I don't
stop
the macros, the second acceptable row is found in Excel, and the charts
are
prepared in Word, but not all commands of the formatting logic is
processed,
and upon returning to Excel to go after the 3rd acceptable row, I get a
message "Method 'Range' of object '_global' failed".
Is it possible the Excel macro is not waiting for the Word macro to
finish?
What things should I look out for in this scenario?
"Bernie Deitrick" wrote:
Bill,
Strings must be wrapped in double quotes, not single:
wordApp.Run ("myMacro", "String with spaces", numVal1, numVal2)
and your macro in the word document must be properly declared, along the
lines of:
Sub myMacro(myStr As String, myVal1 As Double, myVal2 As Double)
HTH,
Bernie
MS Excel MVP
"Bill Sturdevant" wrote in
message
...
I have a macro in Excel that calls a macro in a Word document with this
code:
wordApp.Visible = True
wordApp.Activate
wordApp.Run ("myMacro")
But when I changed the Word macro to require parameters, using the
following
code, I get Run-time error '-2147352573 (80020003)' Unable to run the
specified macro:
wordApp.Visible = True
wordApp.Activate
wordApp.Run ("myMacro", 'String with spaces', numVal1,
numVal2)
So I tried using the following code, but I get Object doesn't support
this
property or method:
wordApp.Visible = True
wordApp.Activate
result = wordApp.Run ("myMacro", 'String with spaces',
numVal1,
numVal2)
What code should I be using?
|