本文將介紹 3 種有效的方法,幫助您取代 Word 文件頁首中的文字。 
對於某些文件而言,頁首是整個文件單元的重要組成部分。大多數情況下,整個文件的頁首文字保持一致,因為文件中的每個頁首都彼此連結。然而,也有例外情況,例如文件可能包含多個部分,而這些部分的頁首彼此之間沒有連結。
以下是根據特定情況選擇的3種方法。
方法1:僅用一個部分替換文檔中的標題文本
通常,整個文檔都是一個部分,除非您手動對其進行劃分。 在這種情況下,很容易更改標題中的文本。
- 首先,雙擊標題區域進入編輯模式。
- 然後替換文本並單擊“關閉頁眉和頁腳”命令以退出。
每個標題中的相同文本現在已更改。
方法2:用取消鏈接的標題替換多節文檔中的標題文本
這是第二種情況,其中文檔通常包含多個部分。 默認情況下,每個部分的標題都在“鏈接到上一個”中設置。 但是,出於某些需求,您可以取消鏈接標頭以在文檔中創建不同的標頭。
即使這樣,仍然會有一些文本在所有標題中保持不變。 在這種情況下要替換它們,您將必須使用following宏來完成工作。
- 首先,通過按“ Alt + F11”打開VBA編輯器。
- 其次,單擊左側欄中的“普通”項目。
- 第三,單擊“插入”,然後在下拉菜單上選擇“模塊”。
- 接下來,雙擊模塊以將其打開並將這些代碼粘貼到此處:
Sub ReplaceTextInHeaderInADoc()
Dim nPageNum As Integer
Dim objDoc As Document
Dim strFindText As String
Dim strReplaceText As String
Set objDoc = ActiveDocument
strFindText = InputBox("Enter text to be found:", "Find Text")
strReplaceText = InputBox("Enter new text:", "Replace Text")
For nPageNum = 1 To Selection.Information(wdNumberOfPagesInDocument)
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=nPageNum
Application.Browser.Target = wdBrowsePage
objDoc.Bookmarks("\page").Range.Select
With objDoc.ActiveWindow
.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With .Selection.Find
.ClearFormatting
.Text = strFindText
.Replacement.ClearFormatting
.Replacement.Text = strReplaceText
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End With
Next nPageNum
objDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
- 然後按“ F5”運行宏。
- 在“查找文本”框中,輸入要查找和替換的文本。
- 然後單擊“確定”繼續。
- 現在,在“替換文本”框中,輸入新文本,然後單擊“確定”繼續。
方法3:在多個文檔中替換標題文本
儘管如此,我們仍需要宏來進行批處理。
- 首先,將所有目標文件放在同一個資料夾中。
- 重複方法2中的步驟以安裝和運行宏。
- 然後用這個替換宏:
Sub ReplaceTextInHeaderInMultiDoc()
Dim StrFolder As String
Dim strFile As String
Dim objDoc As Document
Dim dlgFile As FileDialog
Dim strFindText As String
Dim strReplaceText As String
Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
With dlgFile
If .Show = -1 Then
StrFolder = .SelectedItems(1) & "\"
Else
MsgBox "Please select the target folder."
Exit Sub
End If
End With
strFindText = InputBox("Enter text to be found:", "Find Text")
strReplaceText = InputBox("Enter new text:", "Replace Text")
strFile = Dir(StrFolder & "*.docx", vbNormal)
While strFile <> ""
Set objDoc = Documents.Open(FileName:=StrFolder & strFile)
For nPageNum = 1 To Selection.Information(wdNumberOfPagesInDocument)
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=nPageNum
Application.Browser.Target = wdBrowsePage
objDoc.Bookmarks("\page").Range.Select
With objDoc.ActiveWindow
.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With .Selection.Find
.ClearFormatting
.Text = strFindText
.Replacement.ClearFormatting
.Replacement.Text = strReplaceText
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End With
Next nPageNum
objDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
objDoc.Save
objDoc.Close
strFile = Dir()
Wend
End Sub
- 在「瀏覽」視窗中,選擇放置文件的目標位置。
- 點擊“確定”。
- 然後在“查找文本”和“替換文本”框中,輸入文本,然後單擊“確定”。
備份失敗時
我們經常建議人們定期備份。 但是,備份可能會不時失敗。 這是備份失敗,媒體損壞和備份過程中損壞的常見原因。 因此,下次備份文檔時,請注意兩個方面。 最好的預防措施應該是 docx恢復 提前使用工具。
作者簡介:
陳薇薇(Vera Chen)是 DataNumen,Inc.是數據恢復技術的全球領導者,包括 XLS修復 以及 pdf 維修軟件產品。 欲了解更多信息,請訪問 萬維網。datanumen.COM




