C#操作Word的内涵:C#操作Office套件的时候都需要用到COM组件,需要在项目里添加相应的COM组件。用C#在Word里查找指定关键字的方法有两种:Selection对象和Range对象。
C#操作Word之Selection对象的用法:
// C#
internal void SelectionFind()
{
string strFind = "find me";
Word.Find fnd = ThisApplication.Selection.Find;
fnd.ClearFormatting();
fnd.Text = strFind;
object missingValue = Type.Missing;
if (fnd.Execute(ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue))
{
MessageBox.Show("Text found.");
}
else
{
MessageBox.Show("The text could not be located.");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
C#操作Word之Range对象的用法:
// C#
internal void RangeFind()
{
Word.Range rng = ThisDocument.Paragraphs[2].Range;
Word.Find fnd = rng.Find;
fnd.ClearFormatting();
object missingValue = Type.Missing;
object findStr = "find me";
if (fnd.Execute(ref findStr,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue))
{
MessageBox.Show("Text found.");
}
else
{
MessageBox.Show("Text not found.");
}
rng.Select();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
C#操作Word的一点体会就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。
【编辑推荐】