PDA

View Full Version : Copy and Paste From One Document Into Another



ShaunW
01-15-2021, 09:41 AM
Hi Folks,
I am looking for some help with creating a macro to copy a lot of text from one document into another.
I am not asking for anyone to write the whole thing for me - just point me in the right direction into whether this is possible and if so how I could start it.

In the original document I have text within tables that isn't content controlled and in the document I would like to paste it too it would be content controlled.
The original document looks a little like the top table, and the second document looks like a little like the bottom table.

27739

Is this something that is possible to achieve? To essentially copy the text underneath heading 1 from the first document into content control F1.1 (which is underneath heading 1 in the second document), the text under heading 2 into content control F1.2 etc?

Any push in the right direction with this would be fantastic.

gmaxey
01-15-2021, 10:12 AM
Well yes, I think something would be possible. As a stab:


Sub StabAtIt()
Dim oDocS As Document, oDocT As Document
Dim lngIndex As Long
Dim oRng As Range
Set oDocS = Documents(1) 'The doc with the text to copy
Set oDocT = Documents(2) 'The doc with the CCs
Set oRng = oDocS.Range
With oRng.Find
.Style = "Heading 1"
While .Execute
lngIndex = lngIndex + 1
oRng.Move wdParagraph, 1
oDocT.SelectContentControlsByTitle("F1." & lngIndex).Item(1).Range.Text = oRng.Paragraphs(1).Range.Text
Wend
End With
lbl_Exit:
Exit Sub
End Sub