尝试使用Interop.Word突出显示完整的短语

时间:2017-04-17 11:34:53

标签: c# .net

我正在努力尝试,但我无法承受最后的事情来结束我的申请。

我有一些短语存储为字符串,例如"有些东西只是"。 而现在我的节目将突出显示所有" Something is just",还有所有" Something",all"是"和所有"只是"在Word .docx文本中。

我不知道如何使它成为可能,因为现在我使用我的文件除以文字。我不知道我是否应该使用其他foreach类型,或者只有一种Range方法可以帮助我解决这个问题。

这是我的代码:

int number;
  number =  (int) (Math.ceil(0) * 1);
  System.out.println("number generated was: " + number);

keyList有三个字符串。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

此解决方案基于使用Word's Range.Find object在文档文本中查找短语的位置,并设置任何找到的项目的HighlightColorIndex属性。

public static void WordHighliter(Word.Document doc, IEnumerable<string> phrases, Word.WdColorIndex color)
            {
            Word.Range rng = doc.Content;
            foreach (string phrase in phrases)
                {
                rng = doc.Content;
                Word.Find find = rng.Find;

                find.ClearFormatting();
                find.Text = phrase;
                find.Forward = true;
                find.Wrap = Word.WdFindWrap.wdFindStop;
                find.Format = false;
                find.MatchCase = false;
                find.MatchWholeWord = true;
                find.MatchWildcards = false;
                find.MatchSoundsLike = false;
                find.MatchAllWordForms = false;
                find.MatchByte = true;

                while (find.Execute())
                    {
                    Int32 start = rng.Start;
                    // ensure that phrase does not start within another word
                    if (rng.Start == rng.Words[1].Start)
                        {
                        rng.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                        }
                    }
                }

            }