Results 1 to 5 of 5

Thread: [RESOLVED] Cut and Paste between two open Word Documents

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Resolved [RESOLVED] Cut and Paste between two open Word Documents

    Hi, looking for some assistance please. Only an amateur coder writer.

    I am writing code to that ultimately creates word document that is a result of cutting and pasting the content of multiple word documents into a final document.

    The structure of my code is 1) a user opens a word template that initializes a user form (located in my normal.dot file) where the user enters a series of data and at the end of this process a blank document is created and save with a particular name and in particular location, for the sake of my question, I will refer to this as “Final.doc” 2) once “Final.doc” is saved, in calls up a second user form which presents a list of documents the user can choose from to ultimately created a final document that has the chosen documents merged onto one. Once the users choice is made, my code is required to open the first chosen documents, cut the entire documents content and then paste it into Final.doc. This process continues until all the chosen documents have been cut and pasted until the Final.doc contains all the content required.

    All the code works fine in all areas except for the ability to paste the contents into Final.doc.

    My extracted code is attached (I used hard coded path and files name in this example but in actual code these are variables).

    The chosen file opening ok, the copy function works ok, however when I try to make “Final.doc” active to paste the content into it, the code fails with run-time error ‘5941’. The requested member of the collection does not exist.

    Can I please get any suggestions how, when I have two files open a) Final.doc and b) the chosen document open, to switch back and forth while I cut and paste the content. Much appreciated. John
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Cut and Paste between two open Word Documents

    when I have two files open a) Final.doc
    as your code closes all documents that are open in the same instance of word (documents.close) without saving? after the first iteration through the listbox the final.doc will not be still open, to close just the chosen doc, try changing to
    Code:
    documents(Me.ListBox2.List(mrg)).close
    EDIT:
    when working with multiple documents it is much safer and good practice to assign the opened documents to document object variables, better than just hoping the right document is the currently active one
    Last edited by westconn1; Dec 9th, 2017 at 05:49 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Cut and Paste between two open Word Documents

    Thanks for the suggestion. I have replaced the line you suggest and this solved the issue of the final.doc closing unsaved mid code. However, the code still stops at the "Windows("Final.doc").Activate line with the run time error noted above and thus it still can't paste. Do you have any further suggestions on this.

    Much appreciated John.

    PS I originally was using object variables however was facing the same issue of not being able to make final.doc active so tried to code I attached.

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Cut and Paste between two open Word Documents

    I have played around more with object variables and have got it to work.
    Thanks

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Cut and Paste between two open Word Documents

    i do not know why it should not work for you as i tested here, without problem

    you can try any of these variations
    Code:
    application.Windows("Final.doc").Activate
    documents("Final.doc").activate
    documents("Final.doc").windows(1).activate
    i also note an error in the code you posted before
    For spec = 0 To Me.ListBox2.ListCount - 1
    Documents.Open ("C:\Users\jcurt\Desktop\Technical_Specifications\" & Me.ListBox2.List(mrg))
    you are not using the incrementing variable for your file opener, this may have been a typo or already fixed, but as it may try to open an invalid file it could cause issues
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width