2017-07-06 114 views
0

我对这个网站相当陌生,这是我的第一篇文章。在这里,我们去: 我正在VBA上写一个相当简单的代码,它不需要任何东西来替换某个特定的链。我已经找到了这一部分,但是我想在包含子文件夹的大文件夹中的所有文件上操作该代码....我在前面看到类似的问题,但它对我不起作用因为我试图在代码中输入“If”来验证文件是否是文件,如果是,则调用操作(secondSubRoutine)谁在文件上执行所需的任务。如果有人能告诉我为什么它无法找到文件的文件,这将是非常有益的!在文件夹和所有子文件夹中的所有文字文件上操作任务VBA

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 
+1

[递归枚举文件夹和文件(https://stackoverflow.com/documentation/vba/990/scripting-filesystemobject/10411/recursively-enumerate-folders-和文件#t = 20170706200346311142)使用FSO,在Documentation.SO。 –

回答

1

试试这个:

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 
相关问题