使用C#从任何office文件中删除所有VBA代码

时间:2016-02-29 10:54:56

标签: c# .net excel vba ms-word

我想使用c#从任何office文档中删除所有VBA代码,我试图使用codeproject中的代码:

string file = @"D:\AmlanSandbox\MacroRemoval\
    OfficeDocMacroUtility\OfficeDocMacroUtility\FileTank\Jhinku.docm";
object objTypeMissing = Type.Missing;
object filePath = file;
Microsoft.Office.Interop.Word.ApplicationClass wordAppl = 
    new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = null;
try
{
     doc = wordAppl.Documents.Open(ref filePath, ref objTypeMissing, 
        ref objTypeMissing, ref objTypeMissing, ref objTypeMissing, 
        ref objTypeMissing, ref objTypeMissing, ref objTypeMissing, 
        ref objTypeMissing, ref objTypeMissing, ref objTypeMissing, 
        ref objTypeMissing, ref objTypeMissing, ref objTypeMissing, 
        ref objTypeMissing, ref objTypeMissing);
     if (doc.HasVBProject)
     {
       wordAppl.OrganizerDelete(file, "NewMacros", 
    Microsoft.Office.Interop.Word.WdOrganizerObject.wdOrganizerObjectProjectItems);
     }
     doc.Close(ref objTypeMissing,ref objTypeMissing,ref objTypeMissing);
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    wordAppl = null;
    doc = null;
}

但它没有做好这项工作。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您正在关闭文档,但是您没有保存它。您需要保存更改。尝试在doc.Close之前添加一行,如:

doc.Save();