What is Word Macro?
It is a batch processing commander which enables users to program for realizing some functions to operate Word document more easily and efficiently.
How to create Word Macro?
Take Microsoft Office 2010 as example. Open a document and click View tab to find Macros. Select View Macros and then a dialog box named Macros pops up. Give the macro a name and click Create button. Then, a Visual Basic editor appears and you can program to realize functions what you want. Of course, you can run, edit and delete existing macros.
How to open and save a Word document with macros (.docm) programmatically?
In following solutions, a .NET Word component is used for realizing functions to load and save .docm document quickly and easily.
Initialize a Document class instance and invoke LoadFromFile(string filename, FileFormat fileFormat) method to load .docm document.
Document document = new Document();
Dim document As New Document()
document.LoadFromFile(“E:\Work\Documents\.NET Framework.docm”, FileFormat.Docm)
Invoke SaveToFile(string fileName, FileFormat fileFormat) of Document class to save loaded .docm document. Of course, you can save this file as .doc, .docx format as well.
Invoke ClearMacros() method of Document class directly to remove macros from document.
P.S The .NET Office component, Spire.Office for .NET can be used to realize this function as well.