【问题标题】:How do I find and replace text in the footer of a word document in a VB.net application?如何在 VB.net 应用程序中查找和替换 word 文档页脚中的文本?
【发布时间】:2019-12-03 00:23:07
【问题描述】:

我正在编写一个 VB.net 应用程序,它采用 Word 文档作为模板,并在文档中查找和替换多个标签。我可以很好地为正文文本执行此操作,但我无法使其适用于文档页脚中的文本。我怎样才能做到这一点?

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

【问题讨论】:

  • 由于您对 Stack Overflow 相当缺乏经验:通常单击回答问题的贡献旁边的复选标记。这为寻求类似问题帮助的其他人提供了有用的信息,告诉那些回答问题的人已经解决了问题......并向回答者奖励“积分”。获得足够积分后,您还可以对任何您认为有帮助的贡献(问题或答案)进行投票,这有助于问答存储库引导人们寻找有用的信息。
  • 没问题 :-) 在某些时候我们都是!

标签: vb.net search ms-word footer document


【解决方案1】:

一个Word文档有很多“故事”,Document.Content就是其中之一,是文档的主体。为了解决其他“故事”,有必要访问那些Ranges

使文档页眉和页脚的工作复杂化的事实是,可以有三种不同类型的页眉和页脚,其中两种可以特定于文档的每个Section。大多数文档只有一个部分,但一旦更改页面方向、报纸栏数或页边距,文档就会获得额外的部分。默认情况下,它们的页眉和页脚是“链接的”,所以它们都是一样的。但如果它们需要不同(例如,横向比纵向宽,因此位置可以改变),则需要取消页眉和页脚的链接,并分别进行搜索。

因此,您可能不需要循环遍历这些部分,或者循环中的所有三个 Range.Find,但为了完整起见,我将它们包括在内。

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

【讨论】:

    【解决方案2】:

    这就是最终的工作。

    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 rngFooter As Word.Range
            For Each sec In oDoc.Sections
                rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
                rngFooter.Find.Execute(FindText:=findWord, ReplaceWith:=replaceWord, Replace:=Word.WdReplace.wdReplaceAll)
                rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
                rngFooter.Find.Execute(FindText:=findWord, ReplaceWith:=replaceWord, Replace:=Word.WdReplace.wdReplaceAll)
                rngFooter = sec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
                rngFooter.Find.Execute(FindText:=findWord, ReplaceWith:=replaceWord, Replace:=Word.WdReplace.wdReplaceAll)
            Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 2019-05-20
      • 2016-01-26
      • 2017-08-06
      • 2021-10-13
      • 1970-01-01
      • 2019-08-27
      • 2021-04-27
      相关资源
      最近更新 更多