Extract text from source word document and add text to extracted text and insert in other existing word document.

Eric Melgers 0 Reputation points
2023-07-04T07:02:41.0133333+00:00

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

Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
943 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. John Korchok 5,466 Reputation points
    2023-07-04T15:14:55.1266667+00:00

    Your code shows you finding text, but not copying or pasting it.

    You have the right idea idea of manipulating ranges to add text. You can do that before you copy, which could later the source document, or you can do it after you paste. This Microsoft page has more details about using ranges to edit text: https://learn.microsoft.com/en-us/office/vba/word/concepts/working-with-word/working-with-range-objects

    To mark source text, you could also use bookmarks, which won't require visible alterations to your source file.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.