• Reference to a doc (Word 2002)

    Author
    Topic
    #409660

    I am creating a report in Word programmatically, using automation from Access. During the report creation, I get to jump between 2 or 3 procedures several times, during which I lose my reference to my Word document. I used the following code to establish the reference: [indent]


    Set Wrd = New Word.Application
    Set doc = Wrd.Documents.Add(“c:template.dot”)


    [/indent]
    Notice that I’m opening a file based on a template. At this point in my code, the document hasn’t been saved, yet. My question is: Can I make the reference persistent between procedures, or, if not, how do I re-establish the reference to that specific file that has remained open?

    Thanks for your help.

    Viewing 1 reply thread
    Author
    Replies
    • #874798

      If you declare a variable as Public at the top of your module, and then instantiate it in one procedure, it will be accessible in all procedures and “stay alive” for some period of time, which is more or less the life of the application. I don’t use Access enough to know whether the report process constitutes one application or whether that variable will be reinitialized every time you use a function. Certainly it’s worth a try.

      • #874832

        Thanks for the response. I really appreciate your time.

        You are right about declaring a variable as Public in the declarations section of the module makes the variable available throughout the module. I had done this when declaring my Wrd and doc variables. However, the references to the objects are broken once I leave that module. I’m assume that simple values persist, but references to objects do not. Here is generally what happens:

        Procedure 1 calls Procedure 2. Procedure 2 sets Wrd and doc to their objects and transfers content to those objects. Procedure 2 then ends and control is returned back to Procedure 1. I would have thought that the reference would have persisted back to Procedure 1. This is not the case.

        I even changed the code around so that the references were set in Proc 1 and the processing using those references occur in Proc 2. Either way, I lose the references when moving between procedures.

        Does anyone have any other ideas?

      • #874833

        Thanks for the response. I really appreciate your time.

        You are right about declaring a variable as Public in the declarations section of the module makes the variable available throughout the module. I had done this when declaring my Wrd and doc variables. However, the references to the objects are broken once I leave that module. I’m assume that simple values persist, but references to objects do not. Here is generally what happens:

        Procedure 1 calls Procedure 2. Procedure 2 sets Wrd and doc to their objects and transfers content to those objects. Procedure 2 then ends and control is returned back to Procedure 1. I would have thought that the reference would have persisted back to Procedure 1. This is not the case.

        I even changed the code around so that the references were set in Proc 1 and the processing using those references occur in Proc 2. Either way, I lose the references when moving between procedures.

        Does anyone have any other ideas?

        • #874840

          In theory, at least, public Word objects are no different than public ADO objects (such as connections and recordsets) which happily persist across procedures and modules in the same project. Are the two procedures in the same project? If Procedure1 is in an Access database and Procedure2 is in a Word template, that would be a big challenge! However, within the same project, unless you set the doc variable to nothing or otherwise destroy the object, it should persist.

          As for alternatives, unless you save the document and store its file name somewhere, I’m not sure how you can reliably get back to it. Even with the document name, GetObject could have problems if there are multiple instances of Word open.

          • #874877

            Again, thanks for your time.

            All of my code is in the same Access .mdb module. I have “Watched” my variables as I have stepped through the procedure. As soon as I exit one procedure and return to another, the references on the watched variables turn to “out of context”.

            Although I still have some testing to do, I think I’ve found a workaround to this problem by saving the doc prior to the end of the procedure and then using the Set doc = Documents.Open method to open it back up once I need it again. There’s got to be a cleaner way to do that, but I think it works.

            • #875027

              If I’m understanding what you’re trying to do, I’d think you could simply declare the Word document object at the beginning of Procedure 1 and pass it By Reference to Procedure 2. In other words, it should be a ByRef argument (Optional if you like) of Procedure 2, rather than an object variable that is declared in Procedure 2. (In Access, arguments are ByRef by default.)

              Sub Procedure1()

              Dim oDoc As Word.Document

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              oDoc.Close
              Set oDoc = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              ‘Do stuff with oDoc here.

              Set Wrd = Nothing

              End Sub

            • #875028

              If I’m understanding what you’re trying to do, I’d think you could simply declare the Word document object at the beginning of Procedure 1 and pass it By Reference to Procedure 2. In other words, it should be a ByRef argument (Optional if you like) of Procedure 2, rather than an object variable that is declared in Procedure 2. (In Access, arguments are ByRef by default.)

              Sub Procedure1()

              Dim oDoc As Word.Document

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              oDoc.Close
              Set oDoc = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              ‘Do stuff with oDoc here.

              Set Wrd = Nothing

              End Sub

            • #875564

              Oops. As a follow-up to my previous post, I should note that the sample code I provided failed to Quit the Word application. You could fix this a couple of ways:

              1. You can supplement Procedure 1 with the 4 oApp lines shown here:

              Sub Procedure1()

              Dim oDoc As Word.Document
              Dim oApp As Word.Application

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              Set oApp = oDoc.Application

              oDoc.Close
              oApp.Quit

              Set oDoc = Nothing
              Set oApp = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              ‘Do stuff with oDoc here.

              Set Wrd = Nothing

              End Sub

              2. Or, if it isn’t otherwise incompatible with what you’re doing in these procedures, a more straightforward approach might be to set up the Word.Application completely (and add the document) in Procedure1 and just have Procedure2 manipulate the document, like this:

              Sub Procedure1()

              Dim Wrd As Word.Application
              Dim oDoc As Word.Document

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              oDoc.Close
              Wrd.Quit

              Set Wrd = Nothing
              Set oDoc = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              ‘Do stuff with oDoc here.

              End Sub

              3. Or you could do a hybrid that Set Wrd in Procedure1 but didn’t Documents.Add until Procedure2 — in which case you’d need to also feed Procedure2 Wrd as a 2nd ByRef argument.

            • #875565

              Oops. As a follow-up to my previous post, I should note that the sample code I provided failed to Quit the Word application. You could fix this a couple of ways:

              1. You can supplement Procedure 1 with the 4 oApp lines shown here:

              Sub Procedure1()

              Dim oDoc As Word.Document
              Dim oApp As Word.Application

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              Set oApp = oDoc.Application

              oDoc.Close
              oApp.Quit

              Set oDoc = Nothing
              Set oApp = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              ‘Do stuff with oDoc here.

              Set Wrd = Nothing

              End Sub

              2. Or, if it isn’t otherwise incompatible with what you’re doing in these procedures, a more straightforward approach might be to set up the Word.Application completely (and add the document) in Procedure1 and just have Procedure2 manipulate the document, like this:

              Sub Procedure1()

              Dim Wrd As Word.Application
              Dim oDoc As Word.Document

              Set Wrd = New Word.Application
              Set oDoc = Wrd.Documents.Add(“c:template.dot”)

              Call Procedure2(oDoc:=oDoc)
              ‘Do more stuff with oDoc here.

              oDoc.Close
              Wrd.Quit

              Set Wrd = Nothing
              Set oDoc = Nothing

              End Sub

              Sub Procedure2(Optional oDoc As Word.Document) ‘in a different module

              ‘Do stuff with oDoc here.

              End Sub

              3. Or you could do a hybrid that Set Wrd in Procedure1 but didn’t Documents.Add until Procedure2 — in which case you’d need to also feed Procedure2 Wrd as a 2nd ByRef argument.

          • #874878

            Again, thanks for your time.

            All of my code is in the same Access .mdb module. I have “Watched” my variables as I have stepped through the procedure. As soon as I exit one procedure and return to another, the references on the watched variables turn to “out of context”.

            Although I still have some testing to do, I think I’ve found a workaround to this problem by saving the doc prior to the end of the procedure and then using the Set doc = Documents.Open method to open it back up once I need it again. There’s got to be a cleaner way to do that, but I think it works.

        • #874841

          In theory, at least, public Word objects are no different than public ADO objects (such as connections and recordsets) which happily persist across procedures and modules in the same project. Are the two procedures in the same project? If Procedure1 is in an Access database and Procedure2 is in a Word template, that would be a big challenge! However, within the same project, unless you set the doc variable to nothing or otherwise destroy the object, it should persist.

          As for alternatives, unless you save the document and store its file name somewhere, I’m not sure how you can reliably get back to it. Even with the document name, GetObject could have problems if there are multiple instances of Word open.

    • #874799

      If you declare a variable as Public at the top of your module, and then instantiate it in one procedure, it will be accessible in all procedures and “stay alive” for some period of time, which is more or less the life of the application. I don’t use Access enough to know whether the report process constitutes one application or whether that variable will be reinitialized every time you use a function. Certainly it’s worth a try.

    Viewing 1 reply thread
    Reply To: Reference to a doc (Word 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: