Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    move between docs (VBA office xp/2000)

    I need to transfer text from one existing word doc to another existing word doc in Win XP, word xp,. The problem I have is in the initial setting of the object variable for the two docs, both of which will already be open, with the source doc active, no other windows open. I've done it before, but now I'm suffering from brain fade, and can't recall at all how to do it. My various attempts using code something like below:

    Dim SourceDoc as document
    Dim DestDoc as document

    set SourceDoc = activedocument
    set Destdoc = activewindow.next.document

    transfer code, etc, etc.

    sometimes works fine, other times gives an "object does not exist error"

    What's the best way to to this?

    Thanks,
    Steve

  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: move between docs (VBA office xp/2000)

    "activewindow.next.document" is not reliable. You can find other posts on this.

    How much information do you have about the documents? As you know, you can retrieve a document from the Documents collection by file name or by numeric index, and you can loop through them looking at their properties before making a commitment. Lots of options, depending on what the VB interpreter is going to know at the critical moment.

    Oh, I guess it goes without saying that you're best off creating the object references at the time the documents are created (Set objDoc = Documents.Add) or opened (Set objDoc = Documents.Open), if possible. If there is an unknown delay between the add/open and the time you want to access the document, you could stash a document property or document variable in the document and then look for it when looping through the Documents collection.

  3. #3
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: move between docs (VBA office xp/2000)

    Which Docs need to be modified is unpredictable, so I've come to the conclusion that having the two open already is the easiest solution. I've used the creating a new doc solution which works fine, but then I need to pull info out of two docs into one. Is there any way to have the routine pause and set the DesDoc variable when I click on the destination doc?

  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: move between docs (VBA office xp/2000)

    You can create a small non-modal UserForm that instructs the user to make the destination document active and then click a continue button. This is not trivial, in that you have to divide up your code differently, but it's the only way I know that makes for a nice user experience. I guess a custom toolbar could be substituted for the UserForm.

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

    Re: move between docs (VBA office xp/2000)

    The other method that looks neat is to provide a userform with two drop down list boxes, each containing the names of all the open documents. One has the label "Source" and the other "Target"

    StuartR

Posting Permissions

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