Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Feb 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Active Document (Word 2000)

    Hi - I am attempting to write a Word macro which has 3 documents open at the same time. What we need to do is search the first document for names which are in a second document and if it finds the name, paste it into a third document. We then have to open 7 additional documents which need to be searched for the same list of names.

    I have the basic macro done but am having trouble making sure the correct document is active for each part of the macro. Since the name of 2 of the documents are never the same, what is the best code to set the name and then make it the active document during the correct portion of the macro?

    Thanks,
    Carol

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Active Document (Word 2000)

    In Macros of this sort it is generally best to avoid using ActiveDocument.

    Define 3 (or more) variables of type Document and set each one to point to the correct document. How are you identifying the 3 documents, is it by name, or something to do with their contents? Your code will look something like...

    <font face="Georgia">Dim docSource as Document
    Dim ...
    ...
    ...
    set docSource = ActiveDocument
    ...
    With docSource.Find
    ...
    End With</font face=georgia>

    etc.

    StuartR

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Active Document (Word 2000)

    Best way is to create a document object for each file at th ebeginning of the code, or as soon as the file names are known, ten refere only to the document objects, instead of the raw file patrhs.

  4. #4
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Active Document (Word 2000)

    Stuart and Howard,

    For my general edification, why is it better to refer to an object than to use a string variable to capture a document name then make it the ActiveDocument? I understand the coding of each -- what makes referring to the object preferable?

    Since there's more than one way to skin a macro, it's this kind of distinction that I would like to understand better.

    Thanks,
    Kim

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Active Document (Word 2000)

    VBA is based on objects, their properties and methods.

    When you manipulate a document it is always accessed via a document object. If you store a pointer to the correct document object then you will access it in a consistent and efficient way. If you use some other characteristic of the document to locate the document object every single time then you are wasting resources. Also things like names can change, the document object will continue to point to the correct document.

    AND

    Making a document active is not necessary to use its methods and properties. This is another waste of resources that can be avoided.

    StuartR

  6. #6
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Active Document (Word 2000)

    Stuart,

    Thanks so much for the lesson. I appreciate it and will take it to heart.

    Kim

Posting Permissions

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