Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
 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 4-Jan-13 22:21pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
choudhary.sumit at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100