none
请教VSTO开发问题:怎样实现搜索功能。 RRS feed

  • 问题

  • 请教VSTO开发问题:怎样实现搜索功能。

    例如在word中,我要搜索一段文本(例如:Hello VSTO),然后把所有符合搜索条件的都用不同颜色标记出来,求助~~

    2011年1月14日 1:12

答案

  • Hi

    一段WORD中查找指定字符的代码,请参考:

    public static void DoSearchAndReplaceInWord()

            {

                Word.Application word = new Word.Application();

                Word.Document doc = new Word.Document();

     

                object missing = System.Type.Missing;

     

                try

                {

                    object fileName = @"C:\myDocument.doc";

                    doc = word.Documents.Open(ref fileName,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing);

     

                    doc.Activate();

                    foreach (Word.Range tmpRange in doc.StoryRanges)

                    {

                        tmpRange.Find.Text = "findme";

                        tmpRange.Find.Replacement.Text = "findyou";

                        tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;

                        object replaceAll = Word.WdReplace.wdReplaceAll;

     

                        tmpRange.Find.Execute(ref missing, ref missing, ref missing,

                            ref missing, ref missing, ref missing, ref missing,

                            ref missing, ref missing, ref missing, ref replaceAll,

                            ref missing, ref missing, ref missing, ref missing);

                    }

                    doc.Save();

                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);

                }

                catch (Exception ex)

                {

                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);

                }

            }


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Paul Zhou 2011年1月20日 4:27
    2011年1月19日 5:49

全部回复

  • dear
    试试用Find方法
    http://technet.microsoft.com/zh-tw/library/ee692875.aspx

    C#操作Word完全功略
    http://tmsoft.lsxy.com/index.php?load=read&id=334
    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年1月14日 1:39
  • 用过Find这个属性。不过execute后不知道怎样获取range对象。

    2011年1月14日 2:00
  • Hi

    一段WORD中查找指定字符的代码,请参考:

    public static void DoSearchAndReplaceInWord()

            {

                Word.Application word = new Word.Application();

                Word.Document doc = new Word.Document();

     

                object missing = System.Type.Missing;

     

                try

                {

                    object fileName = @"C:\myDocument.doc";

                    doc = word.Documents.Open(ref fileName,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing, ref missing,

                        ref missing, ref missing, ref missing);

     

                    doc.Activate();

                    foreach (Word.Range tmpRange in doc.StoryRanges)

                    {

                        tmpRange.Find.Text = "findme";

                        tmpRange.Find.Replacement.Text = "findyou";

                        tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;

                        object replaceAll = Word.WdReplace.wdReplaceAll;

     

                        tmpRange.Find.Execute(ref missing, ref missing, ref missing,

                            ref missing, ref missing, ref missing, ref missing,

                            ref missing, ref missing, ref missing, ref replaceAll,

                            ref missing, ref missing, ref missing, ref missing);

                    }

                    doc.Save();

                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);

                }

                catch (Exception ex)

                {

                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);

                }

            }


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Paul Zhou 2011年1月20日 4:27
    2011年1月19日 5:49