使用 C# 从任何办公文件中剥离所有 VBA 代码
本文关键字:剥离 VBA 代码 文件 任何办 使用 | 更新日期: 2023-09-27 18:36:44
我想使用 c# 从任何办公文档中剥离所有 VBA 代码,我试图从代码项目中使用此代码:
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;
}
但它没有完成这项工作。谁能帮忙?
您正在关闭文档,但未保存它。您需要保存更改。尝试在文档之前添加一行。像这样关闭:
doc.Save();