Consulting

Results 1 to 4 of 4

Thread: Copy & paste from external doc to activedoc using bookmarks

  1. #1
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    2
    Location

    Unhappy Copy & paste from external doc to activedoc using bookmarks

    I'm wanting to copy and paste a selection of text, between bookmark A and bookmark B, from external doc to the active document bookmark C.

    I'm more of a beginner than a beginner and just teaching myself as I go but I can't seem to work this one out. Below is the code I've got that doesn't seem to be working


    • I don't know how to differentiate and set the separate ranges.
    • I don't know the coding to reference each range when copying and pasting


    Some help would be greatly appreciated.

    Sub Test()


    Dim docA As Document
    Dim docB As Document
    Dim Range As Bookmarks
    Dim aRange As Range
    Dim bRange As Range


    Set docA = ActiveDocument
    Set docB = Documents.Open("Z:\xxx.doc")


    Set aRange =
    Set rngStart = docB.Bookmarks("Demo").Range
    Set rngEnd = docB.Bookmarks("EndDemo").Range


    Set bRange = docA.Bookmarks("PasteDemo").Range


    docB.Range.Copy
    docA.Range.Paste


    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You might try something along the lines of:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim DocSrc As Document, DocTgt As Document, RngSrc As Range, RngTgt As Range
    Set DocTgt = ActiveDocument
    Set DocSrc = Documents.Open("Z:\xxx.doc")
    Set RngTgt = DocTgt.Bookmarks("Destination").Range
    With DocSrc
      Set RngSrc = .Range(.Bookmarks("Start").Range.Start, .Bookmarks("End").Range.End)
      RngTgt.FormattedText = RngSrc.FormattedText  
      DocTgt.Bookmarks.Add "Destination", RngTgt
     .Close False
    End With
    Set RngSrc = Nothing: Set RngTgt = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
    Application.ScreenUpdating = True
    End Sub
    Last edited by macropod; 06-19-2018 at 12:34 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    The following should work

    Sub Test()
    
    Dim docA As Document
    Dim docB As Document
    Dim aRange As Range
    Dim bRange As Range
    
        Set docA = ActiveDocument
        Set docB = Documents.Open("Z:\xxx.docx")
        Set aRange = docB.Bookmarks("Demo").Range
        aRange.End = docB.Bookmarks("EndDemo").Range.End
        Set bRange = docA.Bookmarks("PasteDemo").Range
        bRange.FormattedText = aRange.FormattedText
        bRange.Bookmarks.Add "PasteDemo"
        docB.Close 0
        Set docA = Nothing
        Set docB = Nothing
        Set aRange = Nothing
        Set bRange = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    2
    Location
    Thank you so much this worked!



    Quote Originally Posted by macropod View Post
    You might try something along the lines of:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim DocSrc As Document, DocTgt As Document, RngSrc As Range, RngTgt As Range
    Set DocTgt = ActiveDocument
    Set DocSrc = Documents.Open("Z:\xxx.doc")
    Set RngTgt = DocTgt.Bookmarks("Destination").Range
    With DocSrc
      Set RngSrc = .Range(.Bookmarks("Start").Range.Start, .Bookmarks("End").Range.End)
      RngTgt.FormattedText = RngSrc.FormattedText  
      DocTgt.Bookmarks.Add "Destination", RngTgt
     .Close False
    End With
    Set RngSrc = Nothing: Set RngTgt = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
    Application.ScreenUpdating = True
    End Sub

Tags for this Thread

Posting Permissions

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