[2012-10-10][转]C# WORD操作实现代码

#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#VB

object start = 0; 
object end = 0; 

Word.Range rng = this.Range(ref start, ref end); 
rng.Text = "New Text"; 


下面的代码示例可用于应用程序级外接程序。 此代码使用活动文档。

C#VB

Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
rng.Text = "New Text";


选择 Range 对象,该对象已从一个字符扩展为所插入文本的长度。

C#VB

rng.Select();


在范围中替换文本
如果指定的范围包含文本,则该范围内的所有文本将替换为插入的文本。

在范围中替换文本

创建一个 Range 对象,该对象包含文档中的前 12 个字符。

下面的代码示例可用于文档级自定义项。

C#VB

object start = 0; 
object end = 12; 

Word.Range rng = this.Range(ref start, ref end); 


下面的代码示例可用于应用程序级外接程序。 此代码使用活动文档。

C#VB

Word.Range rng = this.Application.ActiveDocument.Range(0, 12);


将这些字符替换为字符串“新文本”。

C#VB

rng.Text = "New Text"; 


选择范围。

C#VB

rng.Select();


使用 TypeText 插入文本
TypeText 方法在选定内容处插入文本。 TypeText 的行为因用户计算机上设置的选项而异。 以下过程中的代码声明一个 Selection 对象变量,然后关闭 Overtype 选项(如果它处于打开状态)。 如果激活了 Overtype 选项,则会覆盖光标旁边的任何文本。

使用 TypeText 方法插入文本

声明一个 Selection 对象变量。

C#VB

Word.Selection currentSelection = Application.Selection; 


如果 Overtype 选项是打开的,则将其关闭。

C#VB

if (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#VB

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 块中的代码不执行任何操作。

C#VB

else
{
    // Do nothing.
}


还可以使用 Selection 对象的 TypeBackspace 方法,该方法模拟键盘上的 Backspace 键的功能。 不过,在需要插入并处理文本时,Range 对象可以为您提供更多的控制。

下面的示例演示完整的代码。 若要使用此示例,请从项目内的 ThisDocument 或 ThisAddIn 类中运行代码。

C#VB

private 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;
}


 
   
  
 
   
  
 
   
  
 
   
  
 
   
  
 
   
  
 

转载于:https://www.cnblogs.com/lema/archive/2012/10/10/2718835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值