以下是 VBA 代码示例,用于打印指定文件夹及其子文件夹中的所有 Word 文档:
```
Sub PrintAllWordDocsInFolder()
' 获取需要打印的文件夹路径
Dim folderPath As String
folderPath = "C:\MyFolder"
' 获取文件夹中的所有 Word 文档
Dim wordDocs As Collection
Set wordDocs = GetAllWordDocsInFolder(folderPath)
' 打印每个 Word 文档
Dim doc As Document
For Each doc In wordDocs
doc.PrintOut
Next doc
End Sub
Function GetAllWordDocsInFolder(ByVal folderPath As String) As Collection
' 创建一个集合来存储所有 Word 文档
Dim wordDocs As New Collection
' 获取文件夹中的所有文件和子文件夹
Dim fileSystem As Object
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Dim folder As Object
Set folder = fileSystem.GetFolder(folderPath)
Dim file As Object
For Each file In folder.Files
' 如果文件是 Word 文档,则将其添加到集合中
If LCase(Right(file.Name, 4)) = ".doc" Or LCase(Right(file.Name, 5)) = ".docx" Then
wordDocs.Add Documents.Open(file.Path)
End If
Next file
Dim subFolder As Object
For Each subFolder In folder.SubFolders
' 递归调用 GetAllWordDocsInFolder 函数以获取子文件夹中的 Word 文档
Dim subFolderWordDocs As Collection
Set subFolderWordDocs = GetAllWordDocsInFolder(subFolder.Path)
Dim subDoc As Document
For Each subDoc In subFolderWordDocs
wordDocs.Add subDoc
Next subDoc
Next subFolder
Set GetAllWordDocsInFolder = wordDocs
End Function
```
在这个示例中,我们首先定义了一个名为 `PrintAllWordDocsInFolder` 的过程,用于打印指定文件夹及其子文件夹中的所有 Word 文档。我们将需要打