解决了一年多的问题,狂喜(一年之前)

 

今天用了一下午的时间,终于解决了C#通过WORD模板来自动生成WORD文档,以便于客户端打印的测试代码.
原理就是在WORD里面写宏,然后在C#里面调用这些宏.
 
晚上测试终于,心里很开心.
 
object oMissing = System.Reflection.Missing.Value;

   Microsoft.Office.Interop.Word.Application  oWord = new Microsoft.Office.Interop.Word.Application(); 
   oWord.Visible = false;
   Microsoft.Office.Interop.Word.Documents  oDocs = oWord.Documents;
   object oFile = @"c:\TestWord.doc";
  

   Microsoft.Office.Interop.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);
   // Run the macros.
   RunMacro(oWord, new Object[]{"test",this.textBox1.Text});
  
   // Quit Word and clean up.
   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("ok");
 
private void RunMacro(object oApp, object[] oRunArgs)
  {
   oApp.GetType().InvokeMember("Run",
    System.Reflection.BindingFlags.Default |
    System.Reflection.BindingFlags.InvokeMethod,
    null, oApp, oRunArgs);
  }
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306683

posted on 2006-11-15 12:25  戴玮  阅读(312)  评论(0编辑  收藏  举报

导航