3 Effective Methods to Replace Text in the Header of Your Word Document

In this post, there will be 3 effective methods allowing you to replace text in the Header of your Word document. Replace Text in the Header of Your Word Document

For some documents, header is an essential component of the whole unit. And most of time, text in header is set the same through the entire document, for each header in document is linked with each other. However, there are exceptions when a document can has multiple sections with delinked headers.

Following are 3 ways you can pick according to specific scenario.

Method 1: Replace Header Text in Document with One Section Only

Normally, a whole document is a section unless you manually divide it. In such a case, it’s easy to change the text in header.

  1. First, double click on header area to enter editing mode.
  2. Then replace text and click “Close Header and Footer” command to exit.Replace Text->Click "Close Header and Footer"

The same text in each header is changed now.

Method 2: Replace Header Text in Document of Multiple Sections with Delinked Headers

Here is the second scenario where a document, more often than not, contains several sections. By default, headers in each section are set in “Link to Previous”. But, out of certain demand, you may delink headers as to create different headers in a document.

Even so, there still can be some text which stays the same in all headers. To replace them under such circumstances, you will have to use following macro to do the job.

  1. First of all, open VBA editor by pressing “Alt+ F11”.
  2. Second, click “Normal” project on the left column.
  3. Thirdly, click “Insert” and also choose “Module” on the drop-down menu.Click "Normal"->Click "Insert"->Click "Module"
  4. Next double click on module to open it and paste these codes there:
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
  1. Then hit “F5” to run the macro.Paste Macro->Click "Run"
  2. In the “Find Text” box, enter the text to be found and replaced.
  3. And click “OK” to move on.Enter Text to be Found->Click "OK"
  4. Now in “Replace Text” box, enter new text and click “OK” to proceed.Enter New Text->Click "OK"

Method 3: Replace Header Text in Multiple Documents

Still, we will need macro to do the batch processing.

  1. Before all, arrange all target documents in the same folder.
  2. Repeat steps in method 2 to install and run a macro.
  3. Then replace macro with this one:
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
  1. In “Browse” window, select the target where you put documents.
  2. Click “OK”.
  3. And in “Find Text” and “Replace Text” boxes, enter texts and click “OK”.

When Backup Fails

Quite often we would suggest people to back up on a regular basis. Yet, backups can fail from time to time. And here are common reasons to backup failures, bad media and corruption in backup process. So, next time you back up documents, pay attention to the two aspects. And the best preventive measure should be to get a docx recovery tool in advance.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including xls fix and pdf repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published.