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!
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!