Reputation: 1
I am fairly new to this site and this is my first post. Here we go: I am writing a fairly simple code on VBA that replace a certain chain of character by nothing. I have that part figured out, however I would like to operate that code on all the files in a big folder containing sub folder on and on.... I saw a similar question asked on here before, but it does not work for me since i'm trying to put in a "If" in the code to verify the file is a word file and if it is then it calls the operation ( secondSubRoutine ) who does the required task on the file. If someone could tell me why it's not capable to find the word files, it would be really helpful!
Aurelion.
Sub domino()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = "C:\CLIENTS"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
If File.Name = "*.docx" Then
Documents.Open FileName:=File
Call secondSubRoutine
ActiveDocument.Save
ActiveDocument.Close
ElseIf File.Name = "*.doc" Then
Documents.Open FileName:=path & File
Call secondSubRoutine
ActiveDocument.Save
ActiveDocument.Close
End If
Next
End Sub
Upvotes: 0
Views: 374
Reputation: 8518
Try this:
For Each File In Folder.Files
If Right(File.Name, 5) = ".docx" Or Right(File.Name, 4) = ".doc" Then
Documents.Open FileName:=File.Path
Call secondSubRoutine
With ActiveDocument
.Save
.Close
End With
End If
Next
Upvotes: 1