Introduction
The following code will enable you to detect the presence of any macro in Word 2007. You can also remove it using the code.
I've used .NET Framework 2.0, Windows 2003 Server, C#, Microsoft Word 2007, Microsoft Word 2003.
In this article, I've described and illustrated the code that I've used for myself. I've not included the code for grouping such functionalities together and making a utility library. The following code can be used in a simple console or Windows based application for testing. This can even be extended to other Microsoft Office products. It will be great if someone comes with a utilty library using this and extend it to other macro enabled files.
Background
During the last assignment, I was asked to build a utility that can effectively detect the presence of any macro in Word 2007 and subsequently remove it from the file.
Using the Code
For using the code, you need to refer to the following COM components from the COM tab (after selecting the add reference dialogue box):
- Microsoft Office 12.0 Object Library
- Microsoft Word 12.0 Object Library
- Microsoft Visual Basic for Applications Extensibility 5.3
For easy understanding of the code, I've taken a step by step approach.
1. Load the Word Document
At first, for detecting the presence of macro, we need to load the Word file using Office and Word specific object library. The following lines of code perform the work:
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. Detect Whether the Document Contains any Macro
The following line returns true
/false
depending upon the presence of macro in the Word document.
doc.HasVBProject
So to make any conditional operation, we can wrap it in an if
clause, something like:
if (doc.HasVBProject)
3. Remove Macro from the Document
Now the final step. If the document contains any macro, we can remove it by using the following lines of code:
wordAppl.OrganizerDelete(file, "NewMacros",
Microsoft.Office.Interop.Word.WdOrganizerObject.wdOrganizerObjectProjectItems);
4. Close the Document
The following line of code closes the opened document and releases the resources associated with the document:
doc.Close(ref objTypeMissing,ref objTypeMissing,ref objTypeMissing);
5. Release Resources
As we are dealing with COM components, it's absolutely necessary to remove the resources associated with the application. To achieve this, use the following lines of code:
wordAppl = null;
doc = null;
6. The Full Code with Try, Catch and Finally Block
So summarizing all the points, the complete code is as follows:
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;
}
Points of Interest
There are lots of avenues that can be exploited in Office object models for automation and doing such work. So it will be great if someone bundles some related functionalities like these (say Excel Macro removal, etc.) or can extend these functionalities in a library.
History
- 10th August, 2010: Initial post