当前位置:.net爱好者技术文章C#技术 → C#封装Word常用完美操作类

C#封装Word常用完美操作类

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2008-6-19 16:18:15 阅读:[]
        #region 文档中的文本和对象
        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;
            // Make sure Overtype is turned off.
            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.
                }
            // Restore the user's Overtype selection
            this.m_WordApp.Options.Overtype = userOvertype;
        }
        #endregion
        #region 搜索和替换文档中的文本
        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.Replacement.ClearFormatting();
            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);
        }

上一页  [1] [2] [3] [4] [5]  下一页