Search code examples
vb.netsearchms-wordfooterdocument

How do I find and replace text in the footer of a word document in a VB.net application?


I am writing an VB.net application that takes a Word document as a template and finds and replaces several tags within the document. I can do this for the body text just fine, but I am not able to get this to work for text in the document's footer. How can I accomplish this?

Imports Microsoft.Office.Interop

Dim oWord As Word.Application
Dim oDoc As Word.Document

Public Sub Create_Report()
    Dim clientName As String = InputBox("Enter the [Client Contact's Name] for the report:")
    Dim empName As String = InputBox("Enter the [Employee's Name] for the report:")

    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add("template.dotx")

    ReplaceTemplateText("<<<Contact>>>", clientName)
    ReplaceTemplateText("<<<Employee>>>", empName )

    oDoc.SaveAs("report.docx")
    oDoc.Close()
    oWord.Quit()

    MsgBox("Report Complete", MsgBoxStyle.OkOnly)
End Sub

Public Sub ReplaceTemplateText(findWord As String, replaceWord As String)
    'Replace text in the Template document with input text

    'Body
    oDoc.Content.Find.Execute(FindText:=findWord, ReplaceWith:=replaceWord, Replace:=Word.WdReplace.wdReplaceAll)

    'Footer
     ???
End Sub

Solution

  • A Word document has many "Stories", of which Document.Content is one, and is the main body of the document. In order to address other "Stories" it's necessary to access those Ranges.

    Complicating the work in document headers and footers is the fact that there can be three different types of header and footer, and of these, two can be specific to each Section of a document. Most documents have only one section, but as soon as the page orientation is changed, or the number of newspaper columns, or the margins a document acquires additional sections. By default, their headers and footers are "linked", so they're all the same. But if they need to be different (landscape orientation is wider than portrait, for example, so positioning can change), then headers and footers need to be unlinked and each of them searched separately.

    So you may not need to loop through the sections, or all three Range.Find in that loop, but I include them for the sake of completeness.

    Public Sub ReplaceTemplateText(findWord As String, replaceWord As String)
        'Replace text in the Template document with input text
    
        'Body
        oDoc.Content.Find.Execute(FindText:=findWord, ReplaceWith:=replaceWord, Replace:=Word.WdReplace.wdReplaceAll)
    
        'Footer
        Dim sec as Word.Section
        Dim rngFooter as Word.Range
        For Each sec in oDoc.Section
           Set rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
           'Do rngFooter.Find.Execute
           Set rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
           'Do rngFooter.Find.Execute
           Set rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
           'Do rngFooter.Find.Execute
        Next
    
    End Sub