2017-04-17 8 views
0

私は試して試しています。私は最後にアプリケーションを終了する余裕がありません。Interop.Wordでフレーズ全体を強調表示しようとしています

私はいくつかのフレーズを文字列として保存しています。例えば、 "Something is just"です。 今、私のプログラムでは、「何かは正しかった」のすべてが強調表示されますが、Wordの.docxテキストには「すべて」「すべて」「すべて」と「すべて」が強調表示されます。

私は文書を単語で区切って使用しているので、どのようにできるか分かりません。私は他のforeach型を使うべきかどうかわからない、あるいはRangeのメソッドがあり、この問題を解決するのに役立つかもしれない。

for (int i = 0; i < keyList.Count; i++) 
{ 
    foreach (Range range in doc.Words) 
    { 
     if (keyList[i].Contains(range.Text.Trim())) 
     { 
      range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow; 
     }  
    } 
} 

Aキーリストは、三単語列を持っています

は、ここに私のコードです。 助けてくれてありがとう!

+0

http://stackoverflow.com/a/41364583/3060520 [私はプログラム的に太字なしで文書全体をWord文書に太字テキストを書き込むにはどうすればよい?](の –

+0

可能な重複http://stackoverflow.com/questions/11564073/how-do-i-write-bold-text-to-a-word-document-program-without-bolding-the) – krillgar

+0

それは私の問題を解決しません:( – Starynowy

答えて

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; 
         } 
        } 
       } 

      } 
+0

本当にうまくいきます! ) – Starynowy

関連する問題