Consulting

Results 1 to 6 of 6

Thread: run Word script from Excel with multiple open documents

  1. #1

    run Word script from Excel with multiple open documents

    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!

  2. #2
    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"

  3. #3
    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?

  4. #4
    note line 12 in the above quote

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Did you try doc2.Activate first?

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Don't use reserved names (e.g. 'paste') for macros. Use "M_paste" instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •