您可以使用下面的代碼來打開一個Word文檔,並突出顯示搜索文本(S):
private void btnFind_Click(object sender, EventArgs e)
{
object fileName = "xxxxx"; //The filepath goes here
string textToFind = "xxxxx"; //The text to find goes here
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
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 docRange in doc.Words)
{
if(docRange.Text.Trim().Equals(textToFind,
StringComparison.CurrentCultureIgnoreCase))
{
docRange.HighlightColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
docRange.Font.ColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
你必須使用下面的語句添加到的Microsoft.Office.Interop.Word參考:
using Word = Microsoft.Office.Interop.Word;
如果你想要做一個函數,然後修改它
這種支持是否將參數作爲參數傳遞? – Bisileesh
這是一個班級,所以你可以通過你想要的任何東西。在構造函數中或通過屬性。 –
我會弄明白的。謝謝! – Bisileesh