在网上搜寻了一些代码,向指定书签中写入内容可以,但是把它读取出来就不行了,能够找到书签的名字(bk.Name),但是书签制定的能容找不到,总是显示null( bk.Range.Text为null),试了好久还是不行啊!

解决方案 »

  1.   

    //获取标签数据
            public string GetDocumentBookData(string FileName)
            {            Microsoft.Office.Interop.Word.Document wDoc = null;
                Microsoft.Office.Interop.Word.Application wApp = null;
                this.OpenWordDoc(FileName,ref wDoc, ref wApp);
                object oEndOfDoc = "\\endofdoc";
                object missing = System.Reflection.Missing.Value;             string str="";            System.Collections.IEnumerator enu=wApp.ActiveDocument.Books.GetEnumerator();            while(enu.MoveNext())
                {
                    Microsoft.Office.Interop.Word.Book bk = (Microsoft.Office.Interop.Word.Book)enu.Current;                str += "{"+bk.Name.ToString() + ":"+bk.Range.Text+"}";
                }            object o = null;            wDoc.Close(ref missing, ref missing, ref missing);
                wApp.Quit(ref missing, ref missing, ref missing);            if (wDoc!=null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
                    wDoc = null;
                }            if (wApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
                    wApp = null;
                }            GC.Collect();            return str;
            }
    这段代码有没有试过,我以前用过,可以的
      

  2.   

    读取的时候,你的word模版必须打开,否则读取出来的就是空的
      

  3.   

    请问楼上的大哥,你的OpenWordDoc方法是怎么读取word模版的?
    oWord.Documents.Open(ref  fileName, ref   confirmConversions, ref   readOnly, ref   addToRecentFiles, ref   passwordDocument, ref   passwordTemplate, ref   revert, ref   writePasswordDocument, ref   writePasswordTemplate, ref   format, ref   encoding, ref   visible, ref   openConflictDocument, ref   openAndRepair, ref   documentDirection, ref   noEncodingDialog)
     我采用上面的方法,在xp系统中能读取word模版,但是在windows 2003中,返回的却是空对象,不知道什么原因,请指教。
      

  4.   

    this.OpenWordDoc(FileName,ref wDoc, ref wApp);
    这是个函数  网上很多!
      

  5.   

    this.OpenWordDoc(FileName,ref wDoc, ref wApp);
    这是个现成的函数 去网上搜搜!
      

  6.   

    if(Doc.Books.Exists("a"))
    {
    object b="a";
    Word.Book bk=Doc.Books.Item(ref b); word.range wordrng = doc.books.get_item(ref j).range; 
    string s=bk.Range.Text ;
    }
    读取和写入书签内容