Nick Farsi
Nick Farsi

Reputation: 427

Call .docm macro from C#

How to invoke a macro from .docm file in C#?

The answer is directly below.

But in order to meet high quality stackoverflow standards I have to write a rant here for this question to be posted.

Upvotes: 0

Views: 287

Answers (1)

Nick Farsi
Nick Farsi

Reputation: 427

using System.IO;
using Microsoft.Office.Interop.Word;

namespace OpenDocmMacro
{
    class Program
    {
        static void Main(string[] args)
        {
            string file_path = "";
            string macro_name = "";
            Application ap = new Application();
            Document document = new Document();
            try
            {
                document = ap.Documents.Open(Path.GetFullPath(file_path));
                ap.Run(macro_name);
            }
            catch
            {

            }
            finally
            {
                document.Close();
                ap.Quit();
            }
        }
    }
}

Upvotes: 1

Related Questions