Я создаю приложение формы C # для окон и хочу найти строку в текущем открытом текстовом документе, и, если она совпадает, я хочу выделить текст в текстовом документе.Также я хочу знать, возможно ли показать окно сообщения на стороне документа word, показывающее сообщение?
Ниже приведен код, который я написал
string matchString = "This is the text I want to match";
public void testMethod()
{
Object wordObject = null;
Application word = null;
object missObj = Missing.Value;
object readOnly = false;
object FullPath = null;
Document document = null;
try
{
wordObject = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
word = (Application)wordObject;
word.Visible = true;
//word.ScreenUpdating = false;
FullPath = word.ActiveDocument.FullName;
document = word.Documents.Open(ref FullPath, ref missObj, ref readOnly, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj);
document.Activate();
foreach (Microsoft.Office.Interop.Word.Range docRange in document.Sentences)
{
string xx = docRange.Text.Trim();
if (docRange.Text.Trim().Equals(matchString,
StringComparison.CurrentCultureIgnoreCase))
{
docRange.HighlightColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
docRange.Font.ColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
}
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}