Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Feb 2010
    Location
    Denver, CO
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reference to a doc (Word 2002)

    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:
    <hr>Set Wrd = New Word.Application
    Set doc = Wrd.Documents.Add("c:template.dot")<hr>
    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.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Reference to a doc (Word 2002)

    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.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2010
    Location
    Denver, CO
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference to a doc (Word 2002)

    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 <font color=blue>Wrd</font color=blue> and <font color=blue>doc</font color=blue> 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?

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Reference to a doc (Word 2002)

    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.

  5. #5
    2 Star Lounger
    Join Date
    Feb 2010
    Location
    Denver, CO
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference to a doc (Word 2002)

    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 <font color=blue>Set doc = Documents.Open</font color=blue> 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.

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Reference to a doc (Word 2002)

    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

  7. #7
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Reference to a doc (Word 2002)

    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.

Posting Permissions

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