- ·上一篇文章:C# 操作Excel的完美类
- ·下一篇文章:列出C#进程以及详细信息
C#封装Word常用完美操作类
#region 文档中的文本和对象
public void AppendText(string text)
{
Selection currentSelection = this.m_WordApp.Selection;
public void AppendText(string text)
{
Selection currentSelection = this.m_WordApp.Selection;
// Store the user's current Overtype selection
bool userOvertype = this.m_WordApp.Options.Overtype;
bool userOvertype = this.m_WordApp.Options.Overtype;
// Make sure Overtype is turned off.
if (this.m_WordApp.Options.Overtype)
{
this.m_WordApp.Options.Overtype = false;
}
if (this.m_WordApp.Options.Overtype)
{
this.m_WordApp.Options.Overtype = false;
}
// Test to see if selection is an insertion point.
if (currentSelection.Type == WdSelectionType.wdSelectionIP)
{
currentSelection.TypeText(text);
currentSelection.TypeParagraph();
}
else
if (currentSelection.Type == WdSelectionType.wdSelectionNormal)
{
// Move to start of selection.
if (this.m_WordApp.Options.ReplaceSelection)
{
object direction = WdCollapseDirection.wdCollapseStart;
currentSelection.Collapse(ref direction);
}
currentSelection.TypeText(text);
currentSelection.TypeParagraph();
}
else
{
// Do nothing.
}
if (currentSelection.Type == WdSelectionType.wdSelectionIP)
{
currentSelection.TypeText(text);
currentSelection.TypeParagraph();
}
else
if (currentSelection.Type == WdSelectionType.wdSelectionNormal)
{
// Move to start of selection.
if (this.m_WordApp.Options.ReplaceSelection)
{
object direction = WdCollapseDirection.wdCollapseStart;
currentSelection.Collapse(ref direction);
}
currentSelection.TypeText(text);
currentSelection.TypeParagraph();
}
else
{
// Do nothing.
}
// Restore the user's Overtype selection
this.m_WordApp.Options.Overtype = userOvertype;
}
#endregion
this.m_WordApp.Options.Overtype = userOvertype;
}
#endregion
#region 搜索和替换文档中的文本
public void Replace(string oriText, string replaceText)
{
object replaceAll = WdReplace.wdReplaceAll;
public void Replace(string oriText, string replaceText)
{
object replaceAll = WdReplace.wdReplaceAll;
this.m_WordApp.Selection.Find.ClearFormatting();
this.m_WordApp.Selection.Find.Text = oriText;
this.m_WordApp.Selection.Find.Text = oriText;
this.m_WordApp.Selection.Find.Replacement.ClearFormatting();
this.m_WordApp.Selection.Find.Replacement.Text = replaceText;
this.m_WordApp.Selection.Find.Replacement.Text = replaceText;
this.m_WordApp.Selection.Find.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);
}
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);
}

