查找并替换word文档页脚中的语法
问题描述:
我需要生成一个包含一些语法的word文档。它找到并替换整个文档,除了页脚。请帮我。谢谢你提前了!
I need to generate a word document with some syntax inside it. It find and replace for the whole document except for the footer. Please help me. Thanks in advanced!
Public Sub WordDocumentReminder4(fileName As Object, NewFileName As Object)
Dim missing As Object = System.Reflection.Missing.Value
Dim wordApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
Dim aDoc As Microsoft.Office.Interop.Word.Document = Nothing
Try
aDoc = wordApp.Documents.Open(fileName, missing, missing, missing, missing, missing, _
missing, missing, missing, missing, missing, missing, _
missing, missing, missing, missing)
Dim Dtable4 As New DataTable
Dtable4 = ObjDBConn.GetDataTable("Exec Aduan.ReminderLetter3DataGET " & _
"'" & txtcomplaintid.Text.ToString & "','" & txtexplanationid.Text.ToString & "'")
aDoc.Activate()
If Dtable4.Rows.Count > 0 Then
Me.FindAndReplace(wordApp, "<<OurRefNo>>", Dtable4.Rows(0).Item("OurRefNo").ToString)
Me.FindAndReplace(wordApp, "<<LetterIssueDate>>", Dtable4.Rows(0).Item("LetterIssueDate").ToString)
Me.FindAndReplace(wordApp, "<<TO>>", Dtable4.Rows(0).Item("TO").ToString)
Me.FindAndReplace(wordApp, "<<AlamatTO>>", Dtable4.Rows(0).Item("AlamatTO").ToString)
Me.FindAndReplace(wordApp, "<<StateTO>>", Dtable4.Rows(0).Item("StateTO").ToString)
Me.FindAndReplace(wordApp, "<<ComplaintNo>>", Dtable4.Rows(0).Item("ComplaintNo").ToString)
Me.FindAndReplace(wordApp, "<<ReminderSubj3>>", Dtable4.Rows(0).Item("ReminderSubject3").ToString)
Me.FindAndReplace(wordApp, "<<ReminderRefNo3>>", Dtable4.Rows(0).Item("ReminderRefNo3").ToString)
Me.FindAndReplace(wordApp, "<<ReminderDate3>>", Dtable4.Rows(0).Item("ReminderDate3").ToString)
Me.FindAndReplace(wordApp, "<<CmpnrName>>", Dtable4.Rows(0).Item("ComplainantName").ToString)
'It did not find this syntax which is inside a footer.
Me.FindAndReplace(wordApp, "<<FooterDate>>", Dtable4.Rows(0).Item("ReminderDate3").ToString)
aDoc.SaveAs(NewFileName, missing, missing, missing, missing, missing, _
missing, missing, missing, missing, missing, missing, _
missing, missing, missing, missing)
End If
aDoc.Close(missing, missing, missing)
wordApp.Quit(Type.Missing, Type.Missing, Type.Missing)
Catch ex As Exception
aDoc.Close(missing, missing, missing)
wordApp.Quit(Type.Missing, Type.Missing, Type.Missing)
End Try
End Sub
Private Sub FindAndReplace(WordApp As Microsoft.Office.Interop.Word.Application, findText As Object, replaceWithText As Object)
Dim matchCase As Object = True
Dim matchWholeWord As Object = True
Dim matchWildCards As Object = False
Dim matchSoundsLike As Object = False
Dim nmatchAllWordForms As Object = False
Dim forward As Object = True
Dim format As Object = False
Dim matchKashida As Object = False
Dim matchDiacritics As Object = False
Dim matchAlefHamza As Object = False
Dim matchControl As Object = False
Dim read_only As Object = False
Dim visible As Object = True
Dim replace As Object = 2
Dim wrap As Object = 1
WordApp.Selection.Find.Execute(findText, matchCase, matchWholeWord, matchWildCards, matchSoundsLike, nmatchAllWordForms, _
forward, wrap, format, replaceWithText, replace, matchKashida, _
matchDiacritics, matchAlefHamza, matchControl)
End Sub
答
@ sern89,你永远不会得到预期的结果,因为你正在搜索并替换Selection.Range
。如果您想在所有文档中进行搜索和替换,则需要定义适当的范围:
如何:以编程方式定义和选择文档中的范围 [ ^ ]
如何:以编程方式在Word中设置搜索选项 [ ^ ]
如何:以编程方式搜索和替换文档中的文本 [ ^ ]
如何自动设置Word和r etrieve节页眉和页脚信息 [ ^ ]
如何:以编程方式在文档中添加页眉和页脚 [ ^ ]
[/ EDIT]
声明和定义FindAndReplace
程序错误。
@sern89, you'll never get expected result because you're searching and replacing insideSelection.Range
. If you would like to search and replace in all document, you need to define proper range:
How to: Programmatically Define and Select Ranges in Documents[^]
How to: Programmatically Set Search Options in Word[^]
How to: Programmatically Search for and Replace Text in Documents[^]
How to automate Word to set and retrieve section header and footer information[^]
How to: Programmatically Add Headers and Footers to Documents[^]
[/EDIT]
Declaration and definition ofFindAndReplace
procedure is wrong.
Private Sub FindAndReplace(WordApp As Microsoft.Office.Interop.Word.Application, findText As Object, replaceWithText As Object)
它应该是:
It should be:
Private Sub FindAndReplace(Worddoc As Microsoft.Office.Interop.Word.Document, findText As String, replaceWithText As String)
由于输入参数的类型,
:
because of the type of input parameters:
.Text = "find me"
.Replacement.Text = "Found"