PDA

View Full Version : run Word script from Excel with multiple open documents



pseudoclever
05-29-2014, 09:53 AM
Hi all. First time poster, long time beneficiary of advice here.

I'm writing a program which opens multiple Word documents from Excel, then calls functions within those documents to fill out various bits of paperwork for me. For various logistical reasons, at one point it's reasonably important that at least two different documents be open at the same time. The problem is, the objword.run method seems to puke out when more than one document is open, even if the function I'm calling only exists in one of the documents. Does anyone have any advice on how to make this work?

Sub TestWord()

Dim openWord As Word.Application

Dim Doc1 As Word.Document

Dim Doc2 As Word.Document

Set openWord = GetObject(, "word.application")

openWord.Visible = True

Set Doc1 = openWord.Documents.Open("C:/Filepath/Testdoc.docm")

Set Doc2 = openWord.Documents.Open("C:/Filepath/Testtwo.docm")

openWord.Run ("Paste")

end sub



Currently the program "Paste" only exists in Testdoc. I get, mystifyingly, error number -2147352573 with this code on the openword.run line. If the program exists in BOTH, I get other errors.

I would REALLY like to be able to give something like:




openword.documents.doc1.run("Paste")



...but depending on the variation I use with that, either the compiler won't allow it, or I get error 438 or 424. Any help would be MOST appreciated. Thanks!

westconn1
05-29-2014, 02:42 PM
is paste private?

from help file

Run Method
Runs a Visual Basic macro.
Syntax
expression.Run(MacroName, varg1, varg2, varg3, varg4, varg5, varg6, varg7, varg8, varg9, varg10, varg11, varg12, varg13, varg14, varg15, varg16, varg17, varg18, varg19, varg20, varg21, varg22, varg23, varg24, varg25, varg26, varg27, varg28, varg29, varg30)
expression Required. An expression that returns an Application object.
MacroName Required String. The name of the macro. Can be any combination of template, module, and macro name. For example, the following statements are all valid.
Application.Run "Normal.Module1.MAIN"
Application.Run "MyProject.MyModule.MyProcedure"
Application.Run "'My Document.doc'!ThisModule.ThisProcedure" If you specify the document name, your code can only run macros in documents related to the current context — not just any macro in any document.
varg1...varg30 Optional Variant. Macro parameter values. You can pass up to 30 parameter values to the specified macro.
Remarks
Although Visual Basic code can call a macro directly (without this method being used), this method is useful when the macro name is stored in a variable (for more information, see the example for this topic). The following statements are functionally equivalent.
Normal.Module2.Macro1
Call Normal.Module2.Macro1
Application.Run MacroName:="Normal.Module2.Macro1"

pseudoclever
05-29-2014, 03:01 PM
Yes, paste is private. The line still executes without issue if only one document is open.

Is there a way to specify a document to execute the function? Or do functions only exist at the application level?

westconn1
05-30-2014, 02:22 AM
note line 12 in the above quote

Kenneth Hobs
06-02-2014, 09:03 AM
Did you try doc2.Activate first?

snb
06-02-2014, 02:34 PM
Don't use reserved names (e.g. 'paste') for macros. Use "M_paste" instead.