Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Global variables (WinNT/Word97)

    I'm sure this has been discussed before, but I can't find it!

    I'm running one template from a document created by another (that part works fine) then, having saved the original doc's path and name to global variables (defined in a module in the original doc), I want to save the new document under the same name with a short suffix, "-ps". ie c:dataoriginal.doc will create c:dataoriginal-ps.doc. This also would work fine IF (and it's a big if) the variable was carried over to the new document.

    It worked when I tested it first (ie when I typed "varfinal" in the second doc with a variable defined in the first doc's module as Public varFinal, "varfinal" changed to "varFinal"); but when I run it it just stops and says varfinal not defined.

    I presume I'm doing something wrong about how/where to define the variable to make it application-global as opposed to just project-global, but what I'm doing wrong is eluding me!!

    Someone please help!! <img src=/S/crybaby.gif border=0 alt=crybaby width=15 height=15>
    Beryl M


  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: Global variables (WinNT/Word97)

    Where is the running code? If the running code is in the first document (or its attached template), the global variable should work. If the running code is in a different project, I think there are two problems: (1) the first document (or its attached template) is out of scope, and (2) even if you could somehow cross into the boundary of the first document, if all running procedures have terminated, shouldn't the variable's value have been lost? Some values seem to linger, but I haven't found the pattern.....

    You probably would have better luck using a document variable or document property in the first document, rather than a VBA variable.

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT/Word97)

    Hi Jefferson, thanks for your help, but it's not so much where the code is that's the problem! The reason the new template is running from the old document is that the second template is that there are some docvariables in the new doc (created from the template) that need the values from the old doc, and it's transferring the info that I'm finding so difficult!

    eg (and this is a demo from memory, obviously I'm not at work at the moment!):
    ---------------
    <pre>Option explicit in a separate module in old doc's template
    Public tsfrName as string
    ---------------
    sub transferInfo (in ThisDocument of old doc's template)
    tsfrName = left(olddoc.Name, len(oldDoc.Name)-4)
    documents.add template:=newtemp.dot ...
    end sub
    ---------------
    sub document_new in ThisDoc of new template
    activedocument.saveas tsfrName + "-ps.doc"
    end sub</pre>

    Now, if I put a stop on the 'end sub' line of transferInfo, it will run transferinfo fine then crash on 'activedocument.saveas', saying the variable is not defined. The sub is still running; I can hover over 'tsfrName' in transferinfo and it will contain the name as specified (ie without the .doc).

    This is obviously a simplified version; there are actually half a dozen bits of info that need to be transferred over, mostly in docvariables in the document or custom properties.

    In order to be able to put all the code in the new doc, I tried referring back to the old doc from the new one, but since I have no inkling of what the old document's name will be or whether the two docs we are working with will be the only ones open, or opened this session, I couldn't find a way that would reliably pick up the old doc - I tried specifying the previous member of the documents collection (this doc's index -1), which would work, then I'd run it again and find the old doc was the next member (this doc's index +1). If several docs had been opened and closed since Word was started the two index numbers could be several digits apart!

    So, the problem is, how do I transfer the contents of a number of variables/properties/docvariables (I don't care which way it's done!!) so that the new document picks them up, without knowing anything about the old document's name?!

    It SHOULD have been really simple - newVar = oldVar in the old document, variable("New") = newVar in the new document and off we go ... Both templates are open and available, neither are being shut down during the operation, all the variables/properties etc are there ... but they will NOT communicate! It doesn't matter how they are defined, they just will not cross the yawning chasm between the two docs!

    I'm absolutely <img src=/w3timages/censored.gif alt=censored border=0> CERTAIN it's a matter of having the variables/properties/whatever defined in the right way and the right place but I can't <img src=/w3timages/censored.gif alt=censored border=0> track down the <img src=/w3timages/censored.gif alt=censored border=0> method to make it <img src=/w3timages/censored.gif alt=censored border=0> <img src=/w3timages/censored.gif alt=censored border=0> <img src=/w3timages/censored.gif alt=censored border=0> <img src=/w3timages/censored.gif alt=censored border=0> do so!

    Aaaargh!
    Beryl M


  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: Global variables (WinNT/Word97)

    I suggest this, unless there's a good reason not to... (and no, object-oriented thinking about encapsulating the self-save functionality inside the new template is not a good reason in my book <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ):

    sub transferInfo (in ThisDocument of old doc's template)
    dim docNew as word.document
    tsfrName = left(olddoc.Name, len(oldDoc.Name)-4)
    Set docNew = documents.add template:=newtemp.dot ...
    ' Save newDoc and populate its document variables from here in olddoc
    docNew.SaveAs tsfrName + "-ps.doc"
    ' ...
    end sub

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT/Word97)

    I think you might have it! I could then assign values to document properties of the newdoc for the variable info that needs to be transferred, and ...

    Before I whizz off to work to check, though - how do I transfer focus to 'docNew' so the code there takes over and leaves it with docNew in front? I haven't tried it yet, but is it something as simple as 'activedocument = docNew'?

    Many thanks again!
    Beryl M


  6. #6
    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: Global variables (WinNT/Word97)

    Not always reliable, but try docNew.Activate to give it focus.

  7. #7
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT/Word97)

    It's working!

    I didn't have to try the activate option - the new template has a form on it that was 'shown' by the last line of document_new, so I just removed that line and put a macro to 'show' it at the end of my olddoc macro and it automatically brought the doc attached to the form to 'the fore'.

    Ta ever so muchly!!
    Beryl M


Posting Permissions

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