c# word操作方法

1 打开word模板文档

object objTrue = true;
                object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false;//word文件不可见
                object oTemplate = l1;//word文件地址
                Object myNothing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

2 打开word文件指定复制第四页

object bMissing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word._Application bWord = new Microsoft.Office.Interop.Word.Application();
                    bWord.Visible = false;
                    object bTemplate = @l4 + Value2 + "\\不动产权籍调查表.docx";
                    Object mybNothing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word._Document bDoc = bWord.Documents.Add(ref bTemplate, ref bMissing, ref bMissing, ref bMissing);
                    Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                    int pageCount = 0;
                    pageCount = bDoc.ComputeStatistics(stat, ref bMissing);//得到文档总页数
                    object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                    object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToNext;
                    object page = 4 + ""; // 页数
                                          //ran1 指到要读页数页尾
                    Microsoft.Office.Interop.Word.Range ran1 = bDoc.GoTo(ref What, ref Which, ref page, ref bMissing);
                    // rand2 为 rand1 的上一页,如果 rand1是首页的话也无妨,这样rand2指向文档头部第一个插入位置
                    Microsoft.Office.Interop.Word.Range ran2 = ran1.GoToPrevious(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
                    object objStart = ran2.End; //页首,
                    object objEnd = ran1.Start; //页尾
                    if (page.Equals("" + pageCount))
                    {
                        objStart = ran1.Start;
                        objEnd = bMissing;
                    }

                    Microsoft.Office.Interop.Word.Range r3 = bDoc.Range(ref objStart, ref objEnd);
                    //复制第一个表格
                    r3.Tables[1].Select();
                    bWord.Selection.Copy();
                 oDoc.Bookmarks.get_Item(ref y1).Range.Text = Value2; 
                    oDoc.Bookmarks.get_Item(ref s1).Range.Paste();
                    //oWord.Selection.Paste();
                    bDoc.Close(false);//关闭
                    bWord.Quit();//释放Word进程

三书签复制

//书签位置插入图片
object s3 = "s3";
object rng2 = oDoc.Bookmarks.get_Item(ref s3).Range;
 oDoc.InlineShapes.AddPicture(l8 + "\\" + Value2 + m2 + ".jpg", ref myNothing, ref objTrue, ref rng2);
//书签位置插入文字
 object y1 = "y1";
oDoc.Bookmarks.get_Item(ref y1).Range.Text = "书签位置插入文字";

 

你可能感兴趣的:(C#)