Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to copy from one document to another

    I need to copy certain parts of a document and paste it in the second column of a table in the other document. I would like to create a macro to do that but have been unable to figure out how to make it jump from one open document to the other. I know I have done it before, but for the life of me I cannot remember how I did it previously. The document I am copying from has two columns but indented instead of using the column feature (you know, pressing CTRL+M).

    I am using Word 2007 and Vista.

    I tried to record the macro, but it did not work.

    Can somebody help me. Thank you in advance.

  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
    In your code, create two document objects, one to refer to each document.

    Code:
    Dim d1 as Word.Document, d2 as Word.Document
    The trick, of course, is how to set them to the intended source and destination. When you are creating the second document anew, it's straightforward:

    Code:
    Set d1 = ActiveDocument
    Set d2 = Documents.Add()
    But if you are opening d2, then you may need to first get the file name (including path) using Dialogs(wdDialogFileOpen) and then use Documents.Open() with that name/path.

    Does that help?

    Warning: The above is untested "air code" and may not be exactly right.

  3. #3
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much for responding so fast.

    I do not want the macro to open any document because I have to go from one document to another and it would be a nightmare to create such a macro.

    Both documents will be open and I want to go to one document, copy the text from the insertion point to the return, and then come back to the other open document and paste the information at the insertion point.

    I had recorded the macro:

    Sub Macro11()
    ActiveWindow.Next.Activate
    Selection.MoveDown Unit:=wdParagraph, Count:=1
    Selection.MoveRight Unit:=wdWord, Count:=6
    Selection.Extend
    Selection.Extend Character:=Chr(13)
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.Copy
    ActiveWindow.Next.Activate
    Selection.PasteAndFormat (wdFormatPlainText)
    Selection.MoveRight Unit:=wdCell
    End Sub

    But when I try to run it, it gives me the message:

    Run-time error 91: Object variable or With block variable not set

    and it highlights

    ActiveWindow.Next.Activate

    and stop.

    Obviously waiting for me to fix it but I can't. Don't know if you can help fix it. As you can see I have very little programming knowledge.

    Again, thank you so much.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    This revised code might get you closer to what you need to do:

    Code:
    Sub CopyFromSecondDoc()
    
       Dim ThisDoc As Document
       Dim OtherDoc As Document
       
       If Documents.Count <> 2 Then
          MsgBox "Must have two documents open!"
          Exit Sub
       End If
       
       Set ThisDoc = ActiveDocument
    
       If ThisDoc = Documents(1) Then
          Set OtherDoc = Documents(2)
       Else
          Set OtherDoc = Documents(1)
       End If
       
       OtherDoc.Activate
       Selection.MoveDown Unit:=wdParagraph, Count:=1
       Selection.MoveRight Unit:=wdWord, Count:=6
       Selection.Extend
       Selection.Extend Character:=Chr(13)
       Selection.MoveLeft Unit:=wdCharacter, Count:=1
       Selection.Copy
       ThisDoc.Activate
       Selection.PasteAndFormat (wdFormatPlainText)
       Selection.MoveRight Unit:=wdCell
       
       
       Set ThisDoc = Nothing
       Set OtherDoc = Nothing
    
    End Sub
    Note that it will only work if you have just the two documents open. You should start out with the insertion point in the location in the first document where you want the copied content to be pasted.

    Don't put this code into either of the two documents; put it in a module in your Normal.dot instead.

    (btw I'm going to be offline for the next several hours, so won't be able to check back on how this did, until later...)

    Gary

  5. The Following 2 Users Say Thank You to Gary Frieder For This Useful Post:

    Alma (2012-04-09),reesjordan (2015-06-16)

  6. #5
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much. It works beautifully.

Posting Permissions

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