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

时间:2013-08-22 13:43:37

标签: 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\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

1 个答案:

答案 0 :(得分:0)

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

this页面上的脚本演示了如何实现这一目标。