Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a Word Doc in a directory and a .dotm file in the same.
word doc has not any macro in it, but all macro are in .dotm file.
so the question is this how can i run macros which are in .dotm file from my c# code?
i have tried so far:

C#
private void btnStart_Click(object sender, EventArgs e)
       {

           object oMissing = System.Reflection.Missing.Value;
           Word.ApplicationClass oWord = new Word.ApplicationClass();
           oWord.Visible = true;
           Word.Documents oDocs = oWord.Documents;
           object oFile = txtFileName.Text;

           Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);


           RunMacro(oWord, new Object[] { "ListTagAll" });
           RunMacro(oWord, new Object[] { "TableTag" });
           RunMacro(oWord, new Object[] { "HyperlinkTag" });
           RunMacro(oWord, new Object[] { "PictureReplace" });
           RunMacro(oWord, new Object[] { "ReplaceParagraph" });
           RunMacro(oWord, new Object[] { "CleanTag" });

           oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
           oDoc = null;
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
           oDocs = null;
           oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
           oWord = null;
           MessageBox.Show("Process Completed...");
       }
       private void btnOpen_Click(object sender, EventArgs e)
       {
           OpenFileDialog fd = new OpenFileDialog();
           //fd.Filter = "*.docx|Word Document";
           fd.ShowDialog();
           if (fd.FileName != "")
           {
               txtFileName.Text = fd.FileName.ToString();
           }
       }
       private void RunMacro(object oApp, object[] oRunArgs)
       {
           try
           {

               oApp.GetType().InvokeMember("Run",
                   System.Reflection.BindingFlags.Default |
                   System.Reflection.BindingFlags.InvokeMethod,
                   null, oApp, oRunArgs);

           }
           catch(Exception ex)
                           {
                               MessageBox.Show(ex.InnerException.ToString());
           }
       }
Posted

1 solution

 
Share this answer
 
Comments
choudhary.sumit 5-Jan-13 4:34am    
i have visited those links already. they are gud for running macros if macro are inbuilt with the doc. but i have to call macros from .dotm file. so i m not getting help from you. please update solution if u can.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900