P
PJY
Hi all, Once again I need to ask for your expertise. I have created a macro
that will open a file, remove the password, find text in the footer, then
replace that text, close the file, and move on to the next file in the folder
and so far it works OK, except I cannot get it to work on the first file in
the folder. Can anyone tell me where I went wrong? Here's what I have so
far:
Sub ReplaceFooter()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As String
PathToUse = "U:\FindReplace\"
On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
myDoc.Unprotect "cmf"
With ActiveWindow.View
..ShowRevisionsAndComments = False
..RevisionsView = wdRevisionsViewFinal
End With
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "(ILLINOIS - STD.)"
.Replacement.Text = "(123456)"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "findme"
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Thanks in advance for your time and sharing of our knowledge!
that will open a file, remove the password, find text in the footer, then
replace that text, close the file, and move on to the next file in the folder
and so far it works OK, except I cannot get it to work on the first file in
the folder. Can anyone tell me where I went wrong? Here's what I have so
far:
Sub ReplaceFooter()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As String
PathToUse = "U:\FindReplace\"
On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
myDoc.Unprotect "cmf"
With ActiveWindow.View
..ShowRevisionsAndComments = False
..RevisionsView = wdRevisionsViewFinal
End With
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "(ILLINOIS - STD.)"
.Replacement.Text = "(123456)"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "findme"
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Thanks in advance for your time and sharing of our knowledge!