亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        用C#實(shí)現(xiàn)在Word文檔中搜索文本

        字號(hào):

        Word的對(duì)象模型有比較詳細(xì)的幫助文檔,放在Office安裝程序目錄,office 2003是在Program Files\Microsoft Office\OFFICE11\2052下,文檔本身是為VBA提供的,在這個(gè)目錄下還可以看到所有的office應(yīng)用程序的VBA幫助。
            打開(kāi)VBAWD10.CHM,看到word的對(duì)象模型,根據(jù)以往的使用經(jīng)驗(yàn),很容易在Document對(duì)象下找到Content屬性,該屬性會(huì)返回一個(gè)文檔文字部分的Range對(duì)象,從這個(gè)對(duì)象中不難取到所有的文檔內(nèi)容,再用string的IndexOf()方法很容易達(dá)到目標(biāo)。
            object filename=""; //要打開(kāi)的文檔路徑
            string strKey=""; //要搜索的文本
            object MissingValue=Type.Missing;
            Word.Application wp=new Word.ApplicationClass();
            Word.Document wd=wp.Documents.Open(ref filename,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue);
            if (wd.Content.Text.IndexOf(strKey)>=0)
            {
             MessageBox.Show("文檔中包含指定的關(guān)鍵字!","搜索結(jié)果",MessageBoxButtons.OK);
            }
            else
            {
             MessageBox.Show("文檔中沒(méi)有指定的關(guān)鍵字!","搜索結(jié)果",MessageBoxButtons.OK);
            }
            不過(guò),這種做法是很勉強(qiáng)的,對(duì)小文檔來(lái)說(shuō),不存在問(wèn)題,對(duì)超長(zhǎng)超大的文檔來(lái)說(shuō),這樣的實(shí)現(xiàn)方法已經(jīng)暗埋bug了,而且是程序級(jí)的bug,因?yàn)檎5臏y(cè)試會(huì)很難發(fā)現(xiàn)問(wèn)題,在使用中導(dǎo)致程序出現(xiàn)什么樣的結(jié)果也很難量化描述。
            其實(shí),在word中已經(jīng)提供了可以用作搜索的對(duì)象Find,在對(duì)象模型上也比較容易找到,對(duì)應(yīng)的說(shuō)明是這樣的:該對(duì)象代表查找操作的執(zhí)行條件。Find 對(duì)象的屬性和方法與“替換”對(duì)話框中的選項(xiàng)一致。從模型上看,F(xiàn)ind對(duì)象是Selection的成員,從示例代碼來(lái)看似乎也是Range的成員,查找Range的屬性,果然如此。于是修改上面的代碼:
            wd.Content.Find.Text=strKey;
            if (wd.Content.Find.Execute(ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue))
            {
             MessageBox.Show("文檔中包含指定的關(guān)鍵字!","搜索結(jié)果",MessageBoxButtons.OK);
            }
            else
            {
             MessageBox.Show("文檔中沒(méi)有指定的關(guān)鍵字!","搜索結(jié)果",MessageBoxButtons.OK);
            }
            這樣似乎也不是,因?yàn)槲抑灰袛嘀付ǖ奈谋臼遣皇窃谖臋n中,而不需要知道它出現(xiàn)了幾次,如果有多個(gè)要搜索的文本,難道每次都進(jìn)行全文檔搜索?假設(shè)我要搜索的文本包含在文檔中,的情況是在文檔開(kāi)頭就包含我要查找的文本,最壞的情況是在文檔的最后包含要查找的文本,如果每次取一部分文檔進(jìn)行判斷,符合條件就結(jié)束本次搜索,就可以避免每次搜索整個(gè)文檔了。模型中的Paragraphs對(duì)象現(xiàn)在派上用場(chǎng)了,再修改一下代碼:
            int i=0,iCount=0;
            Word.Find wfnd;
            if (wd.Paragraphs!=null && wd.Paragraphs.Count>0)
            {
             iCount=wd.Paragraphs.Count;
             for(i=1;i<=iCount;i++)
             {
             wfnd=wd.Paragraphs[i].Range.Find;
             wfnd.ClearFormatting();
             wfnd.Text=strKey;
             if (wfnd.Execute(ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue,ref MissingValue,
             ref MissingValue))
             {
             MessageBox.Show("文檔中包含指定的