Good morning,
I have a word source document. Thesource document contains a button for the user to extract specified data from the source document. After pushing this button the VBA code needs to do the following:
- Copy specific parts of the text of the source document.
- Add text lines before, after or inbetween the copied text
- Open an existing word document and paste all content into that document.
The newly filled document doesn't have to be closed and the user can save it to the location they want.
I already have the following code:
Private Sub CmdButExtract_Click()
Dim cDoc As Word.Document, nDoc As Word.Document
Dim cRng As Word.Range, nRng As Word.Range
Set cDoc = ActiveDocument
Set nDoc = Word.Documents.Open("S:\Standaard protocollen\Functional design specification\Customer specifications.docx")
' Set nDoc = Documents.Add
Set cRng = cDoc.Content
Set nRng = nDoc.Content
cRng.Find.ClearFormatting
With cRng.Find
.Forward = True
.Text = "~"
.Wrap = wdFindStop
.Execute
Do While .Found
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.MoveEndUntil Cset:="#", Count:=Word.wdForward
nRng.FormattedText = cRng.FormattedText
' nRng.InsertBefore Text = "Customer properties"
nRng.InsertParagraphAfter
nRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
.Execute
Loop
End With
End Sub
With this code I can extract text already but I don't know how to add text lines before, after or inbetween the copied text.
In my source document text I have added the symbols ~ and # to mark the specific parts to extract.
Can anyone help me