2012-03-23 68 views
0

我用C#使用Microsoft.Office.Interop.Word。我知道你可以通过使用Range.Font.Bold = 1将文本设置为粗体。我的问题是,我有一个很长的句子,我必须在其中写出一些大胆的话,而不是整个句子。如果我的句子是“您想通过电子邮件向您发送问题的答复吗?”,我想“有回应”要大胆。C#Microsoft.Office.Interop.Word

有了这个例子中,我可以大胆只有一个字(通过全word文档循环):

foreach(Microsoft.Office.Interop.Word.Range w in oDoc.Words) 
{ 
    if (w.Text == "Something") 
     w.Font.Bold = 1; 
} 

但是,这仅仅是一个字,我怎么能大胆两个,三个或更多的连续字在一个句子中。

回答

3

无需遍历整个文档。使用Word.WdReplace.wdReplaceAll,类似这样的东西:

private void SearchReplace() 
{ 
    Word.Find findObject = Application.Selection.Find; 
    findObject.ClearFormatting(); 
    findObject.Text = "find me"; 
    findObject.Replacement.ClearFormatting(); 
    findObject.Replacement.Text = "Found"; 

    object replaceAll = Word.WdReplace.wdReplaceAll; 
    findObject.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); 
} 

你可以阅读更多关于它在这里:http://msdn.microsoft.com/en-us/library/f65x8z3d.aspx

希望它能帮助!

相关问题