Milan Soni
asked on
Getting error while doing mass find n replace in Sub folder
When I am running the below code for n number of document in a folder then it works fine but when i inclde more sub folder with the docs then it shows the error
"Run-time error '5792':
The files appears to be corrupted.
Can someone help? I am not much familier with coding.
Public Sub MassReplace()
With ApplicationFileSearch
.LookIn = "C:\Users\milans\Desktop\P rocedure - Test" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.docx" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' for each file you find, run this loop
For i = 1 To .FoundFiles.Count
' open the file based on its index position
Documents.Open FileName:=.FoundFiles(i), ReadOnly:=False 'I tracked error it is the line where erorr is coming
' search and replace the address
Selection.Find.ClearFormat ting
Selection.Find.Replacement .ClearForm atting
With Selection.Find
.Text = "ABC"
.MatchCase = True
.Replacement.Text = "XYZ"
End With
Selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges
Next i
Else
' if the system cannot find any files
' with the .doc extension
MsgBox "No files found."
End If
End With
End Sub
"Run-time error '5792':
The files appears to be corrupted.
Can someone help? I am not much familier with coding.
Public Sub MassReplace()
With ApplicationFileSearch
.LookIn = "C:\Users\milans\Desktop\P
.SearchSubFolders = True ' search the subfolders
.FileName = "*.docx" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' for each file you find, run this loop
For i = 1 To .FoundFiles.Count
' open the file based on its index position
Documents.Open FileName:=.FoundFiles(i), ReadOnly:=False 'I tracked error it is the line where erorr is coming
' search and replace the address
Selection.Find.ClearFormat
Selection.Find.Replacement
With Selection.Find
.Text = "ABC"
.MatchCase = True
.Replacement.Text = "XYZ"
End With
Selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges
Next i
Else
' if the system cannot find any files
' with the .doc extension
MsgBox "No files found."
End If
End With
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Neil. It really helped. I tweaked a little to add find and replace portion in code.
My pleasure. Glad it worked.
Possibly the error is in the code for that class module.
However, it is also possible that your code is simply not detecting temporary files created by Word, that is files whose name begins with a tilde "~" eg
Open in new window
. Attempts to open these files will result in the error that you report. It may be by chance that such files only exist in your sub-folders. In any case you can try fixing this by re-writing your code to read:Open in new window
However, you may be better off ditching your class module and re-writing the procedure to use Microsoft's FileSystemObject . Something like this code below, which I wrote assuming you are doing this from Excel (not sure why I thought that). If you are doing it from Word itself, then there is no need for the creation of the WordApp variable or the later reference to it. You can just use:
Open in new window
as in your original code.Note that you would need to manually add references to the Word library and to Microsoft Scripting Runtime in the "Tools|References" menu in the VBA editor.
Open in new window