3
如何以編程方式從二進制Microsoft Office文檔中刪除宏(doc
,xls
,ppt
)?如何從二進制MS Office文檔中刪除宏?
我不想做的XML格式(xlsx
,docx
,pptx
)。
我不要想要在辦公室禁用宏,我真的想修改文件並刪除它們的任何宏。
如何以編程方式從二進制Microsoft Office文檔中刪除宏(doc
,xls
,ppt
)?如何從二進制MS Office文檔中刪除宏?
我不想做的XML格式(xlsx
,docx
,pptx
)。
我不要想要在辦公室禁用宏,我真的想修改文件並刪除它們的任何宏。
這是假設您想自動化實際應用程序。
添加引用:
以下應擺脫 你的宏代碼的Excel。自動化Word和PowerPoint將類似。
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
using PPT = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Vbe.Interop;
namespace OfficeMacros
{
class Program
{
static void Main(string[] args)
{
}
static void RemoveMacrosExcel(string fileName, string newFileName)
{
var excel = new Excel.Application();
var workbook = excel.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
foreach (VBComponent component in workbook.VBProject.VBComponents)
{
switch (component.Type)
{
case (vbext_ComponentType.vbext_ct_StdModule):
case (vbext_ComponentType.vbext_ct_MSForm):
case (vbext_ComponentType.vbext_ct_ClassModule):
workbook.VBProject.VBComponents.Remove(component);
break;
default:
component.CodeModule.DeleteLines(1, component.CodeModule.CountOfLines);
break;
}
}
workbook.Close(true, newFileName, Type.Missing);
// Release variables
workbook = null;
excel = null;
// Collect garbage
GC.Collect();
}
}
}
如果你想自己分析二進制文件結構,你需要查看這些文件:
如果這看起來太令人生畏,有一個很棒的圖書館b y DIaLOGIKa調用b2xtranslator(二進制(doc,xls,ppt)到OpenXMLTranslator)。它具有映射到C#對象的.doc,.xls和.ppt的大部分(如果不是全部)二進制結構。
雖然b2xtranslator的意圖是二進制的辦公文檔轉換爲新的OpenXML格式,你可以使用庫來解析文件並刪除宏量元素自己。
+1。現在更簡單了;當我在幾年前(95/97時間)必須要做的時候,微軟需要簽署保密協議才能獲得二進制格式文件:-) – devstuff