Save and Open .Docm Document in C# and Visual Basic

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.

Load

Initialize a Document class instance and invoke LoadFromFile(string filename, FileFormat fileFormat) method to load .docm document.

C#

            Document document = new Document();

            document.LoadFromFile(@”E:\Work\Documents\.NET Framework.docm”,FileFormat.Docm);

Visual Basic

            Dim document As New Document()

            document.LoadFromFile(“E:\Work\Documents\.NET Framework.docm”, FileFormat.Docm)

Save

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.

C#

            document.SaveToFile(“x.docm”, FileFormat.Docm);

Visual Basic

            document.SaveToFile(“x.docm”, FileFormat.Docm)

Clear Macros

Invoke ClearMacros() method of Document class directly to remove macros from document.

C#

            //Clear Macros

            document.ClearMacros();

Visual Basic

            document.ClearMacros()

Download Spire.Doc for .NET Here

P.S The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

Advertisements

One thought on “Save and Open .Docm Document in C# and Visual Basic

  1. save the date originals says:

    Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter
    and said “You can hear the ocean if you put this to your ear.” She placed
    the shell to her ear and screamed. There was a hermit crab inside and it pinched
    her ear. She never wants to go back! LoL I know this is completely off topic but
    I had to tell someone!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s