Diced
04-08-2022, 08:16 PM
Hi, I am having an issue with a FOR EACH loop that iterates through files in a folder and opens them in word, does a search and replace then closes the doc.
Loop works fine and iterates through all the documents but does not exit once they have all been opened it keeps iterating infinitely.
I have removed the search and replace portion of the below code as it is still an infinate loop with that section commented out. Loop works as it should if WITH statements are commented. Any help would be greatly appreciated.
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFileSystem.GetFolder(FolderN)
MsgBox (oFolder.Files.Count)
For Each oFile In oFolder.Files
DName = oFile.Name
With msWord
.Visible = True
.Documents.Open (FolderN & "\" & DName)
.Activate
With .Activedocument
.Save
.Close
End With
End With
Next oFile
Loop works fine and iterates through all the documents but does not exit once they have all been opened it keeps iterating infinitely.
I have removed the search and replace portion of the below code as it is still an infinate loop with that section commented out. Loop works as it should if WITH statements are commented. Any help would be greatly appreciated.
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFileSystem.GetFolder(FolderN)
MsgBox (oFolder.Files.Count)
For Each oFile In oFolder.Files
DName = oFile.Name
With msWord
.Visible = True
.Documents.Open (FolderN & "\" & DName)
.Activate
With .Activedocument
.Save
.Close
End With
End With
Next oFile