录制新宏
![](https://img-blog.csdnimg.cn/img_convert/9552727054507141343bce655f3744bf.png)
将代码替换
![](https://img-blog.csdnimg.cn/img_convert/7693297c12e47f3bb34916a0417d8815.png)
function Macro1()
{
let dirpath="D:\\测试目录\\";
let filepaths=[];
walk(dirpath,filepaths);
for (var f of filepaths){
doc = Documents.Open(f);
change();
doc.Save();
doc.Close();
}
}
//将文件中的文档名取出来
function walk(path,dirs){
let fn = Dir(path, 16);
while (fn){
if (fn !='.' && fn != '..'){
fp = path + fn;
dirs.push(fp);
}
fn = Dir();
}
}
//全局替换内容
function change(){
Selection.Find.Wrap = wdFindContinue;
Selection.Find.Wrap = wdFindContinue;
(obj=>{
//查找的内容
obj.Text = "603130";
obj.Forward = true;
obj.Wrap = wdFindContinue;
obj.MatchCase = false;
obj.MatchByte = true;
obj.MatchWildcards = false;
obj.MatchWholeWord = false;
obj.MatchFuzzy = false;
obj.Replacement.Text = "603703";
})(Selection.Find);
(obj=>{
obj.Style = "";
obj.Highlight = wdUndefined;
(obj=>{
obj.Style = "";
obj.Highlight = wdUndefined;
})(obj.Replacement);
})(Selection.Find);
Selection.Find.Execute(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, wdReplaceAll, undefined, undefined, undefined, undefined);
//替换的内容
Selection.Find.Replacement.Text = "603703";
}
运行宏
![](https://img-blog.csdnimg.cn/img_convert/7310b341d88ba7d51a937d67c2e1db88.png)