C# WORD操作实现代码

来源:互联网 发布:php车辆管理系统 源码 编辑:程序博客网 时间:2024/06/05 00:50
复制代码
#region 拷贝密封线Word        /// <summary>        /// 拷贝密封线Word        /// </summary>        /// <param name="strPath"></param>        private void GetWordContent(object strPath)        {            object strSealWordPath = @"F:\SealWord.docx";            Microsoft.Office.Interop.Word.Application WordAppCopy = null;            Microsoft.Office.Interop.Word.Document WordDocCopy = null;            Microsoft.Office.Interop.Word.Application WordAppPaste = null;            Microsoft.Office.Interop.Word.Document WordDocPaste = null;            Object Nothing = System.Reflection.Missing.Value;            string str = string.Empty;            try            {                WordAppCopy = new Microsoft.Office.Interop.Word.ApplicationClass();                WordDocCopy = WordAppCopy.Documents.Open(ref strSealWordPath, ref Nothing, ref Nothing, ref Nothing,                                                 ref Nothing, ref Nothing, ref Nothing, ref Nothing,                                                 ref Nothing, ref Nothing, ref Nothing, ref Nothing,                                                 ref Nothing, ref Nothing, ref Nothing, ref Nothing);                WordDocCopy.ActiveWindow.Selection.WholeStory();                WordDocCopy.ActiveWindow.Selection.Copy();                WordAppPaste = new Microsoft.Office.Interop.Word.ApplicationClass();                WordDocPaste = WordAppPaste.Documents.Open(ref strPath, ref Nothing, ref Nothing, ref Nothing,                                           ref Nothing, ref Nothing, ref Nothing, ref Nothing,                                           ref Nothing, ref Nothing, ref Nothing, ref Nothing,                                           ref Nothing, ref Nothing, ref Nothing, ref Nothing);                WordDocPaste.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA3;                WordDocPaste.ActiveWindow.Selection.Paste();                WordDocPaste.Save();            }            catch            { }            finally            {                if (WordDocCopy != null)                {                    Marshal.ReleaseComObject(WordDocCopy);                    WordDocCopy = null;                }                WordAppCopy.NormalTemplate.Saved = true;                WordAppCopy.Quit(ref Nothing, ref Nothing, ref Nothing);                if (WordDocPaste != null)                {                    Marshal.ReleaseComObject(WordDocPaste);                    WordDocPaste = null;                }                WordAppPaste.NormalTemplate.Saved = true;                WordAppPaste.Quit(ref Nothing, ref Nothing, ref Nothing);            }        }        #endregion
复制代码
复制代码
可通过三种主要方式将文本插入 Microsoft Office Word 文档:在范围中插入文本。将范围中的文本替换为新文本。使用 Selection 对象的 TypeText 方法可在光标位置或选定内容处插入文本。注意也可以将文本插入内容控件和书签。 有关更多信息,请参见内容控件和Bookmark 控件。适用于:本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。在范围中插入文本使用 Range 对象的 Text 属性在文档中插入文本。在范围中插入文本在文档开头指定一个范围,并插入文本“新文本”。下面的代码示例可用于文档级自定义项。C#VBobject start = 0; object end = 0; Word.Range rng = this.Range(ref start, ref end); rng.Text = "New Text"; 下面的代码示例可用于应用程序级外接程序。 此代码使用活动文档。C#VBWord.Range rng = this.Application.ActiveDocument.Range(0, 0);rng.Text = "New Text";选择 Range 对象,该对象已从一个字符扩展为所插入文本的长度。C#VBrng.Select();在范围中替换文本如果指定的范围包含文本,则该范围内的所有文本将替换为插入的文本。在范围中替换文本创建一个 Range 对象,该对象包含文档中的前 12 个字符。下面的代码示例可用于文档级自定义项。C#VBobject start = 0; object end = 12; Word.Range rng = this.Range(ref start, ref end); 下面的代码示例可用于应用程序级外接程序。 此代码使用活动文档。C#VBWord.Range rng = this.Application.ActiveDocument.Range(0, 12);将这些字符替换为字符串“新文本”。C#VBrng.Text = "New Text"; 选择范围。C#VBrng.Select();使用 TypeText 插入文本TypeText 方法在选定内容处插入文本。 TypeText 的行为因用户计算机上设置的选项而异。 以下过程中的代码声明一个 Selection 对象变量,然后关闭 Overtype 选项(如果它处于打开状态)。 如果激活了 Overtype 选项,则会覆盖光标旁边的任何文本。使用 TypeText 方法插入文本声明一个 Selection 对象变量。C#VBWord.Selection currentSelection = Application.Selection; 如果 Overtype 选项是打开的,则将其关闭。C#VBif (Application.Options.Overtype) {     Application.Options.Overtype = false; } 测试当前选定内容是否是插入点。如果是,则代码会使用 TypeText 插入句子,然后使用 TypeParagraph 方法插入段落标记。C#VB// Test to see if selection is an insertion point.if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) {     currentSelection.TypeText("Inserting at insertion point. ");    currentSelection.TypeParagraph(); } ElseIf 块中的代码测试该选定内容是否为正常选定内容。 如果是,则另一个 If 块将测试 ReplaceSelection 选项是否已启用。 如果已经打开,代码将使用选择的 Collapse 方法将选定内容折叠到选定的文本块开头的插入点。 插入文本和段落标记。C#VBelse     if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)    {         // Move to start of selection.        if (Application.Options.ReplaceSelection)        {             object direction = Word.WdCollapseDirection.wdCollapseStart;            currentSelection.Collapse(ref direction);        }        currentSelection.TypeText("Inserting before a text block. ");        currentSelection.TypeParagraph();    }如果选定内容既不是插入点也不是选定的文本块,则 Else 块中的代码不执行任何操作。C#VBelse{    // Do nothing.}还可以使用 Selection 对象的 TypeBackspace 方法,该方法模拟键盘上的 Backspace 键的功能。 不过,在需要插入并处理文本时,Range 对象可以为您提供更多的控制。下面的示例演示完整的代码。 若要使用此示例,请从项目内的 ThisDocument 或 ThisAddIn 类中运行代码。C#VBprivate void SelectionInsertText() {     Word.Selection currentSelection = Application.Selection;     // Store the user's current Overtype selection    bool userOvertype = Application.Options.Overtype;    // Make sure Overtype is turned off.    if (Application.Options.Overtype)     {         Application.Options.Overtype = false;     }     // Test to see if selection is an insertion point.    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)     {         currentSelection.TypeText("Inserting at insertion point. ");        currentSelection.TypeParagraph();     }     else         if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)        {             // Move to start of selection.            if (Application.Options.ReplaceSelection)            {                 object direction = Word.WdCollapseDirection.wdCollapseStart;                currentSelection.Collapse(ref direction);            }            currentSelection.TypeText("Inserting before a text block. ");            currentSelection.TypeParagraph();        }        else        {            // Do nothing.        }    // Restore the user's Overtype selection    Application.Options.Overtype = userOvertype;}

如何:在文档中扩展范围
如何:检索范围中的开始字符和结束字符
如何:在文档中扩展范围
如何:重置 Word 文档中的范围
如何:折叠文档中的范围或选定内容
如何:创建范围时排除段落标记
 
复制代码
原创粉丝点击