操作word

using WordApplication = Microsoft.Office.Interop.Word.Application;
using Document = Microsoft.Office.Interop.Word.Document;
using Bookmark = Microsoft.Office.Interop.Word.Bookmark;        
  
public void WebPrint()
        {
            String modePath, tempPath;
            modePath = @"c:\model.doc";
            tempPath = @"c:\host.doc";
            WordApplication word = null;
            if (word == null)
            {
                try
                {
                    word = new WordApplication();
                }
                catch   
                {
  
                }
  
                object oMissing = System.Reflection.Missing.Value;
                object path1 = (object)modePath;
                object path2 = (object)tempPath;
                Document doc = null;
                doc = word.Documents.OpenOld(ref path1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                foreach (Bookmark bm in doc.Bookmarks)
                {
                    switch (bm.Name)
                    {
                        case "host":
                            bm.Select();
                            bm.Range.Text="test";
                            break;
                    }
                }
                word.Visible = false;
  
                doc.SaveAs(ref path2, 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);
                doc.PrintPreview();
                doc.Close(ref oMissing, ref oMissing, ref oMissing);
                word.Quit(ref oMissing, ref oMissing, ref oMissing);
                 
                word = null;
                  
            }
posted @ 2014-10-25 15:50  开开123  阅读(166)  评论(0编辑  收藏  举报