如何在 VB.net 应用程序中查找和替换 word 文档页脚中的文本?
How do I find and replace text in the footer of a word document in a VB.net application?
我正在编写一个 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
一个Word文档有很多"Stories"个,其中Document.Content
一个,是文档的主体。为了解决其他 "Stories",有必要访问那些 Ranges
.
使文档页眉和页脚的工作复杂化的事实是,可以存在三种不同类型的页眉和页脚,其中两种可以特定于文档的每个 Section
。大多数文档只有一个部分,但一旦页面方向、报纸栏目数量或页边距发生变化,文档就会获得额外的部分。默认情况下,它们的页眉和页脚是 "linked",所以它们都是一样的。但是,如果它们需要不同(例如,横向比纵向更宽,因此定位可以改变),则需要取消页眉和页脚的链接,并分别搜索它们中的每一个。
因此您可能不需要循环遍历这些部分,或者循环中的所有三个 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
这就是最终的效果。
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
我正在编写一个 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
一个Word文档有很多"Stories"个,其中Document.Content
一个,是文档的主体。为了解决其他 "Stories",有必要访问那些 Ranges
.
使文档页眉和页脚的工作复杂化的事实是,可以存在三种不同类型的页眉和页脚,其中两种可以特定于文档的每个 Section
。大多数文档只有一个部分,但一旦页面方向、报纸栏目数量或页边距发生变化,文档就会获得额外的部分。默认情况下,它们的页眉和页脚是 "linked",所以它们都是一样的。但是,如果它们需要不同(例如,横向比纵向更宽,因此定位可以改变),则需要取消页眉和页脚的链接,并分别搜索它们中的每一个。
因此您可能不需要循环遍历这些部分,或者循环中的所有三个 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
这就是最终的效果。
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