首页 JS清理Word宏代码
文章
取消

JS清理Word宏代码

function cleanMacro(filepath){
//alert("start macro");
//alert(filepath);
var wrd;
//try{
wrd = new ActiveXObject("Word.Application");
wrd.visible=true;
wrd.documents.Open(filepath);
var count = wrd.ActiveDocument.VBProject.VBComponents.Count;
//alert(count);
for(var i=1;i>=count;i++){
try{
var o = wrd.ActiveDocument.VBProject.VBComponents(i);

try{
wrd.ActiveDocument.VBProject.VBComponents.Remove(o);
}catch(e){
//alert("remove comp error:"+e);
try{
var line = o.CodeModule.CountOfLines;
o.CodeModule.DeleteLines(1,line);
}catch(e){
//alert("remove line error:"+e);
}
}
}catch(e){
//alert("macro not exist:"+e);
}

}
wrd.ActiveDocument.Save();
//}catch(e){alert(e);}
try{
wrd.Quit();
}catch(e){}
//alert("end macro");
return true;
}

需要设置IE的安全选项才能运行上面的代码

链接:VBProject:代码操作代码之常用语句

Delete all macros in a workbook/document using VBA in Microsoft Excel

本文由作者按照 CC BY 4.0 进行授权