65.9K
CodeProject 正在变化。 阅读更多。
Home

从 Microsoft Word 2003、Word 2007 中移除宏

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (5投票s)

2010 年 8 月 10 日

CPOL

2分钟阅读

viewsIcon

32217

此实用程序可用于从 Microsoft Word 2007 中删除宏

引言

以下代码可让您检测 Word 2007 中是否存在任何宏。您也可以使用代码将其删除。

我使用了 .NET Framework 2.0、Windows 2003 Server、C#、Microsoft Word 2007、Microsoft Word 2003。

在本文中,我描述并说明了我自己使用的代码。我没有包含将这些功能组合在一起并制作实用程序库的代码。以下代码可以在简单的控制台或基于 Windows 的应用程序中用于测试。甚至可以将其扩展到其他 Microsoft Office 产品。如果有人使用它来创建实用程序库并将其扩展到其他启用宏的文件,那就太好了。

背景

在上一个任务中,我被要求构建一个实用程序,该实用程序可以有效地检测 Word 2007 中是否存在任何宏,然后将其从文件中删除。

Using the Code

要使用该代码,您需要从 COM 选项卡(在选择添加引用对话框后)引用以下 COM 组件

  1. Microsoft Office 12.0 对象库
  2. Microsoft Word 12.0 对象库
  3. Microsoft Visual Basic for Applications Extensibility 5.3

为了便于理解代码,我采取了循序渐进的方法。

1. 加载 Word 文档

首先,为了检测宏的存在,我们需要使用 Office 和 Word 特定的对象库加载 Word 文件。以下代码行执行此工作

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;
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);        

2. 检测文档是否包含任何宏

以下行根据 Word 文档中宏的存在返回 true/false

doc.HasVBProject

因此,要执行任何条件操作,我们可以将其包装在 if 子句中,如下所示

if (doc.HasVBProject) 

3. 从文档中删除宏

现在是最后一步。如果文档包含任何宏,我们可以使用以下代码行将其删除

wordAppl.OrganizerDelete(file, "NewMacros", 
	Microsoft.Office.Interop.Word.WdOrganizerObject.wdOrganizerObjectProjectItems);

4. 关闭文档

以下代码行关闭打开的文档并释放与文档关联的资源

doc.Close(ref objTypeMissing,ref objTypeMissing,ref objTypeMissing);

5. 释放资源

由于我们正在处理 COM 组件,因此绝对有必要删除与应用程序关联的资源。为此,请使用以下代码行

wordAppl = null;
doc = null;

6. 带有 Try、Catch 和 Finally 块的完整代码

因此,总结所有要点,完整的代码如下

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;
}

关注点

Office 对象模型中有很多可用于自动化和执行此类工作的途径。因此,如果有人将一些相关功能(例如 Excel 宏删除等)捆绑在一起,或者可以在库中扩展这些功能,那就太好了。

历史

  • 2010 年 8 月 10 日:初始发布
© . All rights reserved.