Macro to find and replace text in footer in multiple documents in a directory (including unprotecting and reprotecting document if required)

lizziegirl

New Member
Joined
Apr 3, 2006
Messages
31
Hi all
I am struggling to do the above. I need to change a small piece of text in the footer in multiple documents in a fairly big directory. Some may be docs, dots, dotm, docx....etc
Some may be protected, some may not.
I have the below but something is not kicking off.
Do I store this in ThisDocument in a normal.dot?
And where do I place the normal dot?
Sorry for the naivety...
Thanks in advance..
LG


Code:
Public Sub MassReplace()
    Dim Directory As String
    Dim FType As String
    Dim FName As String

    Directory = "T:\Testing"
    FType = "*.dot"

    ChDir Directory
    FName = Dir(FType)
    ' for each file you find, run this loop
    Do While FName <> ""
        ' open the file
        Documents.Open FileName:=FName

        If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If


        ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter


        ' search and replace the text

        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "Looking for this text"
            .MatchCase = True
            .Replacement.Text = "replacing with this text"
        End With
        Selection.Find.Execute Replace:=wdReplaceAll


    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True


        ' save and close the current document
        ActiveDocument.Close wdSaveChanges

        ' look for next matching file
        FName = Dir
    Loop
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Rather than going through all the issues with your code, try:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
Dim Sctn As Section, HdFt As HeaderFooter
Dim StrFndTxt As String, StrRepTxt As String
StrFndTxt = InputBox("What is the Find String?")
StrRepTxt = InputBox("What is the Replace String?")
If Trim(StrFndTxt) = "" Then Exit Sub
strFolder = GetFolder
If strFolder = "" Then Exit Sub
ThisDocument.Range.InsertAfter "Protected files not Processed:" & vbCr
strFile = Dir(strFolder & "\*.do*", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
    AddToRecentFiles:=False, Visible:=True)
  With wdDoc
    If .ProtectionType = wdNoProtection Then
      For Each Sctn In .Sections
        For Each HdFt In Sctn.Footers
          With HdFt
            If .LinkToPrevious = False Then
              With .Range.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Format = False
                .Forward = True
                .Wrap = wdFindContinue
                .Text = StrFndTxt
                .Replacement.Text = StrRepTxt
                .MatchCase = True
                .MatchAllWordForms = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .Execute Replace:=wdReplaceAll
              End With
            End If
          End With
        Next
      Next
    Else
      ThisDocument.Range.InsertAfter strFolder & "\" & strFile & vbCr
    End If
    .Close SaveChanges:=True
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Simply run the macro, answer two questions, then select the folder to process.

The macro outputs a report of protected files that it doesn't process. Do note, though, that processing files with their own macros may cause other problems, such as auto-macros trying to run. Similarly, mailmerge documents will stall the process at the mailmerge prompt. If you have such documents, answer 'yes' at the SQL prompt or you'll kill them as mailmerge documents.

Finally, do not run this macro from a file saved in the folder it is to process - it will politely close itself if you do, quite possible before the folder has finished processing.
 
Upvote 0

Forum statistics

Threads
1,225,681
Messages
6,186,411
Members
453,352
Latest member
OrionF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top