WORD 2007中RUN宏中的问题 [英] Problem in RUN macro in WORD 2007

查看:114
本文介绍了WORD 2007中RUN宏中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个目录中有一个Word Doc和一个.dotm文件。

word doc中没有任何宏,但所有的宏都在.dotm文件中。

所以问题是我如何从我的c#代码中运行.dotm文件中的宏?

i到目前为止已尝试过:



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());
           }
       }

推荐答案

尝试

http://support.microsoft.com/kb/306683 [ ^ ]

http://objectmix.com/csharp/240730-automating-word-macro-c.html [ ^ ]


这篇关于WORD 2007中RUN宏中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆