Quote:
Originally Posted by gmayor
You don't need to copy and paste when using ranges. You can simply write one range to the other. Basically it writes what is in one named bookmark to the other.
It sounds like the source document has nothing in the named bookmark because you are using two bookmarks to set the sources. Bookmark the whole of the text segment and don't use start and end bookmarks.
The use of an Excel source for the data is not necessary.
|
Thanks Greg! This has really helped.
So I have edited the code for the 60 bookmarks I have. I was encountering a runtime error 5941 saying that the bookmarks weren't in the activedocument so I added code into the Object New Document to add the bookmarks. Then when I try to run the code it adds the bookmarks, opens the userform and external doc all without any errors but doesn't copy anything to the activedocument.
This is a sample of the code - I've only included one bookmark as to include them all would be far too long.
HTML Code:
Private Sub CommandButton1_Click()
Dim DocSrc As Document
Dim DocTgt As Document
Dim RngSrc As Range
Dim RngTgt As Range
Set DocTgt = ActiveDocument
Set DocSrc = Documents.Open("C:\Users\reception.AIRDG\Documents\Custom Office Templates\2019 Master Specification - Kate.dotm")
If CheckBox1.Value = True Then
Set RngSrc = DocSrc.BookMarks("A").Range
Set RngTgt = DocTgt.BookMarks("A").Range
RngTgt.FormattedText = RngSrc.FormattedText
RngTgt.BookMarks.Add "A"
End If
DocTgt.Range.Fields.Update
Application.ScreenUpdating = True
Set DocSrc = Nothing
Set DocTgt = Nothing
Set RngSrc = Nothing
Set RngTgt = Nothing
End Sub