Option Explicit Const c_strRootFolder = "D:要处理的doc或docx文件" ' 包含需要处理文档的文件夹 On Error Resume Next Dim oApp, oDoc Dim fso, oFolder, oFile Set fso = CreateObject("Scripting.FileSystemObject") Set oFolder = fso.GetFolder(c_strRootFolder) Set oApp = CreateObject("Word.Application") oApp.Visible = True For Each oFile In oFolder.Files If (UCase(Right(oFile.Path, 4)) = ".DOC") Or (UCase(Right(oFile.Path, 5)) = ".DOCX") Then Set oDoc = oApp.Documents.Open(oFile.Path) oApp.Run "要执行的宏" '执行宏 oDoc.Close True End If Next oApp.Quit False Set oFolder = Nothing Set fso = Nothing Set oApp = Nothing MsgBox "完成!"
注意事项:
1、宏提前写好,保存在word模板中。
2、只适用于windows。
3、将该代码保存为.vbs文件,右键“打开”运行即可。运行结束,会弹窗“完成”。