Results 1 to 8 of 8
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Returning a reference to new document (2000 sp-3/ 98SE)

    I'm wondering how to set a reference to the document that is created, when the Document_New() procedure runs from a template. I can use code from the template to refer to the ActiveDocument, which is fine if the new document remains the active one. And I can use ThisDocument in the template code to refer to the template itself. But how can I set a reliable reference that always points to the new document?

    thanks
    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Returning a reference to new document (2000 sp-3/ 98SE)

    The moment Document_New is run, the new document is always the ActiveDocument. So if you use something like

    Dim doc As Document
    Set doc = ActiveDocument

    at the beginning of Document_New, doc will refer to the new document even if it isn't the active document any more later on in the code.

    If you create the document in code, you can use

    Set doc = Documents.Add(Template:=...)

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Returning a reference to new document (2000 sp-3/ 98SE)

    Elegantly simple solution, as always. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>
    Thanks Hans.

    Alan

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Returning a reference to new document (2000 sp-3/ 98SE)

    OK, I'm having some problems implementing this. I'm using it in the Document_Open event (existing document) and I want "doc" to have global (project) scope. I have the declaration:

    Public doc As Document

    in ThisDocument or another module (don't know which is appropriate, so tried both approaches) and the

    Set doc = ActiveDocument <font color=448800>' or ThisDocument</font color=448800>

    in the Document_Open event. It sets alright when the document opens, but doesn't have global scope (=Nothing when used subsequently in other procedures). Any ideas, please?

    Alan

  5. #5
    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: Returning a reference to new document (2000 sp-3/ 98SE)

    Based on a little experimentation, I'd say the declaration of the variable should be in a standard module, not the ThisDocument module.

    Are you declaring the variable in the document itself, or in the template the document is based on?

    Is the code that sees the variable as Nothing in a project that doesn't have a Reference set to the project where you've declared the variable?

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Returning a reference to new document (2000 sp-3/ 98SE)

    I've tried the declaration in both options, and it doesn't seem to "stick". At this stage, it's all within the one document (no templates involved) and the "Nothing" value occurs within the same project as the ThisDocument module. There is another template referenced by the project, but things don't get that far.

    I just discovered that if I replace the "doc" references with "ThisDocument" then everything works as expected - in both projects. So it certainly points to the doc reference not being seen as project-scoped.

    Alan

  7. #7
    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: Returning a reference to new document (2000 sp

    How are subsequent procedures called? I was thinking that maybe it was global during its lifetime but its life ended. You could try declaring it as Static, although I don't remember very well how that works...

  8. #8
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Returning a reference to new document (2000 sp

    I think Static is relevant to only procedure and module level scopes. But I think you're also right about its lifetime ending. So I may have solved it now, by declaring it public in a normal module, including a sub in that same module to set its value, then calling that sub from the Document_Open event.

    thanks for the help chaps
    Alan

Posting Permissions

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