Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Copy contents to a closed Word document

    Hi,

    Im putting together a macro that saves the content of a document to another document held in a different location. Here's the code Ive put together so far;


    Sub Copy_Content()
    Dim targDoc As Word.Document
    Set targDoc = Application.Documents.Open("c:\sol\test141113.doc" )
    Selection.WholeStory
    Selection.copy
    targDoc.Select
    Selection.PasteAndFormat wdPasteDefault
    End Sub

    For some reason this opens the target document, but doesnt copy over the text from the originating document and Im not sure where Im going wrong. Id be grateful if someone could give me some assistance if possible.

    Many Thanks

    Lee

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,898
    Thanks
    0
    Thanked 188 Times in 172 Posts
    The problem is that you're opening the target document, which becomes the active document, before you copy anything. Try:
    Code:
    Sub Copy_Content()
    Dim targDoc As Word.Document
    ActiveDocument.Range.Copy
    Set targDoc = Application.Documents.Open("c:\sol\test141113.doc")
    targDoc.Range.PasteAndFormat wdPasteDefault
    End Sub
    Note that there's no need to select anything.

    PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Paul,

    Thats great, thanks very much for your response, it now works as expected.

    I wasnt aware of the coding tags, but will ensure I use them in future.

    Best Regards
    Lee

Posting Permissions

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