Reference to already open Word document

  • Hello,


    I am fairly new to Office VBA, so please be patient with me... :)


    Using VBA from Excel 2010, I am opening a Word document, to enter some tekst in some of the bookmarks of this Word document.
    By clicking on the first button everything works fine:



    The Word document remains open while the user goes to a second page of the form, enters some information and presses a second OK-button.
    Now below code does NOT work as the file is already open. A second word document is opened, where the info is added, while I want it to be added in the already open document.
    What needs to be changed to refer to the already open document?


  • Re: Reference to already open Word document


    The simplest answer would be to scope the Word application and document variables to the module this code runs from... or (worst case), public to the entire project.


    For example, if running in a UserForm then declare them at the top of the UserForm code window in the General Declarations section before any other code.


    As ButtonStep0 is the first procedure to run, leave the code as it is. This starts Word and loads the document.


    When buttonStep1 is run, both the Word Application and the WordDoc variable will still exist so the code changes to


    Similarly, any other procedures to update the doucment do not have to start Word, open the document etc...


    As I said, simplistic. there are 'better' (but more complicated to code) ways to do this, but that should work.

  • Re: Reference to already open Word document


    Thanks you.
    That works just as I need it to work!


    No idea if there are 'better' ways to do this, but this is perfect.
    Thanks again.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!