PDA

View Full Version : [SOLVED:] Copy & paste from external doc to activedoc using bookmarks



spaceman
06-18-2018, 09:32 PM
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

macropod
06-19-2018, 12:07 AM
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

gmayor
06-19-2018, 12:11 AM
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

spaceman
06-20-2018, 06:42 PM
Thank you so much this worked!




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