Search code examples
vbams-wordinsert

How to insert content from a different file at the end of a particular heading anywhere in the document? vba


My objective is to insert content form a different file at the end of particular section that is titled "Terms and Conditions" with H1 heading style applied.

The doc structure is as follows:

Exec Summary

Solution Overview

Terms and Conditions

Why Us

Now the following Word VBA code inserts content from a different file at the end of "Terms and Conditions" regardless of where my cursor is located.

Sub insert_doc3()
    Dim doc As Document
    Dim rng As Range
    Dim fileName As String
    
    ' Set the file path of the document containing the content to be inserted
    fileName = "C:\Users\username\Desktop\NCC.docx"
    
    ' Assign the current document to the 'doc' variable
    Set doc = ActiveDocument
    
    ' Find the specified heading in the document
    Set rng = doc.Range
    With rng.Find
        .text = "Terms and Conditions"
        .Style = wdStyleHeading1
        .MatchWholeWord = True
        .Execute
    End With
    
    ' Check if the heading is found
    If rng.Find.Found Then
        ' Move the range to the end of the found heading
        rng.Collapse wdCollapseEnd
        ' Enter a line break and redefine the range to its endpoint
        rng.InsertAfter text:=vbCr
        rng.Collapse Direction:=wdCollapseEnd
        ' Insert the content from the external file
        rng.InsertFile fileName
    Else
        MsgBox "Heading not found."
    End If
End Sub

The code above works just fine, but if I have a document that has a variations of the Terms and Conditions headings as follows, it doesn't quite work.

Exec Summary

Solution Overview

Terms and Conditions of Sale

Why Us

In this case, the content from the new file is inserted after "Terms and Conditions" with "of Sale" following the inserted content, as follows:

Exec Summary

Solution Overview

Terms and Conditions 

<NEW CONTENT>

of Sale

Why Us

How can I fix this so that the content is only inserted if the section is titled "Terms and Conditions".


Solution

  • Sub insert_doc3()
        Dim doc As Document
        Dim rng As Range
        Dim fileName As String
        
        ' Set the file path of the document containing the content to be inserted
        fileName = "C:\Users\username\Desktop\NCC.docx"
        
        ' Assign the current document to the 'doc' variable
        Set doc = ActiveDocument
        
        ' Find the specified heading in the document
        Set rng = doc.Range
        rng.Select
        With Selection.Find
            .Text = "Terms and Conditions"
            .Style = wdStyleHeading1
            .MatchWholeWord = True
            .Execute
        End With
        
        ' Check if the heading is found
        If Selection.Find.Found Then
            ' Move the range to the end of the found heading
            Selection.Expand wdParagraph    'inserted
            Selection.Collapse wdCollapseEnd
            ' Enter a line break and redefine the range to its endpoint
            Selection.InsertAfter Text:=vbCr
            Selection.Collapse Direction:=wdCollapseEnd
            ' Insert the content from the external file
            Selection.InsertFile fileName
        Else
            MsgBox "Heading not found."
        End If
    End Sub
    
    

    Expanding the selection for the paragraph include the full header.