对文件夹和子文件夹中的所有文件执行宏

问题描述

我想对文件夹和子文件中的所有doc和docx文件执行Word宏。我的代码仅执行文件夹的宏(不执行子文件夹的宏)。有没有办法做到这一点?

到目前为止,我的宏是

WITH j AS (
  SELECT table_schema,table_name,count(*) AS count_columns
  FROM @R_268_4045@ion_schema.columns
  GROUP BY table_schema,table_name
) 
SELECT 
  nspname AS schemaname,relname,reltuples,count_columns
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
JOIN j ON j.table_name = relname AND j.table_schema = nspname 
WHERE 
  nspname NOT IN ('pg_catalog','@R_268_4045@ion_schema') AND
  relkind='r' 
ORDER BY reltuples DESC;

解决方法

转到“工具”->“引用”,然后在其中添加Microsoft脚本运行时。这将使您可以访问文件系统对象(FSO)。它包含许多有用的功能,例如获取子文件夹。

然后,您可以使用以下代码遍历它们:

Sub myFunction()

Dim FSO As New FileSystemObject
Dim myFolder As Folder
Dim anotherFolder As Folder
Dim myFile as File

Set myFolder = FSO.GetFolder("C:\a\")

For Each anotherFolder In myFolder.SubFolders
    Debug.Print anotherFolder
    For Each myFile In anotherFolder.Files
        'Here the myFile object will allow you to open each file in the current folder
         Documents.open myFile.Path
         'Do more stuff,then save and close the file
    Next
Next

End Sub

请记住,这只会深入一层。如果需要更深入,则需要加入几个 For 循环。