Macros 在word中运行宏时如何包含子目录

Macros 在word中运行宏时如何包含子目录,macros,ms-word,Macros,Ms Word,我有以下宏代码。但是,它不通过子目录。有人能告诉我如何编辑它吗 Sub ChangeTemplates() Dim strDocPath As String Dim strTemplateB As String Dim strCurDoc As String Dim docCurDoc As Document ' set document folder path and template strings strDocPath = "C:\Users\servicedesk\Desktop\Te

我有以下宏代码。但是,它不通过子目录。有人能告诉我如何编辑它吗

Sub ChangeTemplates()
Dim strDocPath As String
Dim strTemplateB As String
Dim strCurDoc As String
Dim docCurDoc As Document

' set document folder path and template strings
strDocPath = "C:\Users\servicedesk\Desktop\Test\"
strTemplateB = "C:\Users\servicedesk\Desktop\Blanco.dotx"

' get first doc - only time need to provide file spec
strCurDoc = Dir(strDocPath & "*.doc")

' ready to loop (for as long as file found)
Do While strCurDoc <> ""
    ' open file
    Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
    ' change the template
    docCurDoc.AttachedTemplate = strTemplateB
    ' save and close
    docCurDoc.Close wdSaveChanges
    ' get next file name
    strCurDoc = Dir
Loop
MsgBox "Finished"
End Sub
子变更模板()
作为字符串的Dim strDocPath
将strTemplateB变为字符串
作为字符串的Dim strCurDoc
Dim docCurDoc作为文档
'设置文档文件夹路径和模板字符串
strDocPath=“C:\Users\servicedesk\Desktop\Test\”
strTemplateB=“C:\Users\servicedesk\Desktop\Blanco.dotx”
'获取第一个文档-仅需要提供文件规范的时间
strCurDoc=Dir(strDocPath&“*.doc”)
'准备循环(只要找到文件)
当strCurDoc“”时执行
'打开文件
设置docCurDoc=Documents.Open(文件名:=strDocPath&strCurDoc)
'更改模板
docCurDoc.AttachedTemplate=strTemplateB
“保存并关闭
docCurDoc.Close wdSaveChanges
'获取下一个文件名
strCurDoc=Dir
环
MsgBox“已完成”
端接头

您需要修改脚本,将循环文件的部分提取到它自己的函数中,然后在遇到文件夹时递归调用此函数

第页的脚本演示了如何实现这一点