public string Path
{
get
{
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
return info.Parent.Parent.FullName + "\\WordData\\Table.docx";
}
}
//【C# 在word文档中复制表格并粘帖到下一页中】
private void button1_Click(object sender, EventArgs e)
{
object missing = Missing.Value;
Word.Application app = new Word.Application();
app.Visible = true;
//导入模板
object filename = Path;
Word.Document doc = app.Documents.Add(ref filename, missing, missing, missing); //复制第一个表格
doc.Tables[].Select();
app.Selection.Copy();
//在这里操作表格的文本
Word.Cell cellOne = doc.Tables[].Cell(, );
cellOne.Range.Text = "这是第一个表格";
cellOne.Range.Bold = ;
cellOne.Range.Font.ColorIndex = Word.WdColorIndex.wdRed; //下一页
object myunit = Word.WdUnits.wdStory;
app.Selection.EndKey(ref myunit, ref missing);
object pBreak = (int)Word.WdBreakType.wdPageBreak;
app.Selection.InsertBreak(ref pBreak); //粘贴第一个表格
app.Selection.Paste(); //操作第二个表格单元格
Word.Cell cellTwo = doc.Tables[].Cell(, );
cellTwo.Range.Text = "这是第二个表格";
cellTwo.Range.Underline = Word.WdUnderline.wdUnderlineDash;
} //【C#实现WORD文档的内容复制和替换】
private void button2_Click(object sender, EventArgs e)
{
LocalPathHelper pathHelper = new LocalPathHelper();
string sourceWord = WordPath.GetWordDataFullFileName("copy.docx");
string targetWord = pathHelper.DesktopPath() + "\\target.docx";
//复制文件
Word.Document doc = copyWord(sourceWord);
//查找替换 ReplaceAndSave(doc, targetWord);
} //复制word内容到Document对象
public Word.Document copyWord(object sourcePath)
{
object objDocType = Word.WdDocumentType.wdTypeDocument;
object type = Word.WdBreakType.wdSectionBreakContinuous;
object missing = Missing.Value; Word.Application app = new Word.Application();
Word.Document doc; object readOnly = false;
object isVisible = false; doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Word.Document opendWord = app.Documents.Open(ref sourcePath, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref isVisible, ref missing, ref missing, ref missing, ref missing);
opendWord.Select();
opendWord.Sections[].Range.Copy(); object start = ;
Word.Range newRange = doc.Range(ref start, ref start); //插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
doc.Sections[].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
opendWord.Close(ref missing, ref missing, ref missing);
return doc;
} //替换复制好的内容
public void ReplaceAndSave(Word.Document doc, object savePath)
{
object format = Word.WdSaveFormat.wdFormatDocument;
object missing = Missing.Value;
object readOnly = false;
object isVisible = false; string strOldText = "{Word}";
string strNewText = "{提花后的文本}";
List<string> listStr = new List<string>();
listStr.Add("{Word1}");
listStr.Add("{Word2}"); Word.Application app = new Word.Application();
//Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Word.Document newDoc = doc; object FindText, ReplaceText, ReplaceAll;
foreach (string str in listStr)
{
newDoc.Content.Find.Text = str;
//要找的文本
FindText = str;
//替换文本
ReplaceText = strNewText;
//wdReplaceAll - 替换找到的所有项。
//wdReplaceNone - 不替换找到的任何项。
//wdReplaceOne - 替换找到的第一项。
ReplaceAll = Word.WdReplace.wdReplaceAll;
//移除Find的搜索文本和段落格式设置
newDoc.Content.Find.ClearFormatting(); if (newDoc.Content.Find.Execute(ref FindText, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref ReplaceText, ref ReplaceAll, ref missing, ref missing, ref missing, ref missing))
{
MessageBox.Show("替换成功");
}
else
{
MessageBox.Show("没有相关替换的:'" + str + "'字符");
}
} newDoc.SaveAs2(ref savePath);
//关闭文档对象,关闭组件对象
newDoc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);
}

Word复制和替换实例的更多相关文章

  1. [No000051]如何去掉word复制过来的文字背景色?

    我们经常从网上copy一些自己需要的材料到word里.不过常常会把别人的背景色一起拷贝过来.那么如何去掉word复制过来的文字背景色? 方法/步骤 第一步选ctrl+A(全选),找到页面布局→页面边框 ...

  2. ZeroclipboardJS+flash实现将内容复制到剪贴板实例

    Zeroclipboard 的实现原理 Zeroclipboard 利用 Flash 进行复制,之前有 Clipboard Copy 解决方案,其利用的是一个隐藏的 Flash.但最新的 Flash ...

  3. 烂泥:【解决】word复制windows live writer没有图片

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 在使用windows live writer发表博客,博客先是在是word2013中进行编辑,编辑完毕后我会复制到windows live writer ...

  4. Word中批量替换软回车

    在平时工作中,有时候需要拷贝一些截取自网页上的文字,当选中后拷贝到Word中时,有时候在每行的结尾出现如下的符号,,这给后期文字的整理带来了很多不便,在此记录从网上获取的解决方法,以免遗忘和便于查找. ...

  5. jquery插入,复制、替换和删除节点

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. Python正则表达式如何进行字符串替换实例

    Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...

  7. Python之word文档替换字符串(也可以用于短模板套用)

    Python之word文档替换字符串(也可以用于短模板套用),代码如下: 1 ''' 2 #word模板套用1:创建模板,适合比较短的文档 3 ''' 4 5 #导入所需库 6 from docx i ...

  8. c#调用word com组件 替换书签套打

    安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...

  9. 富文本框编辑器实现:a、支持图片复制粘贴;b、支持word复制粘贴图文。

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

随机推荐

  1. 关于Verilog 中的for语句的探讨

    在C语言中,经常用到for循环语句,但在硬件描述语言中for语句的使用较C语言等软件描述语言有较大的区别. 在Verilog中除了在Testbench(仿真测试激励)中使用for循环语句外,在Test ...

  2. 自动文档摘要评价方法:Edmundson,ROUGE

    自动文档摘要评价方法大致分为两类: (1)内部评价方法(Intrinsic Methods):提供参考摘要,以参考摘要为基准评价系统摘要的质量.系统摘要与参考摘要越吻合, 质量越高. (2)外部评价方 ...

  3. mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次

    (总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...

  4. 子类化窗口控件的窗口过程(系统级替换,与直接替换控件的WndProc方法不是一回事)

    要说灵活性,自然是比不上Delphi自带的覆盖WndProc,或者替换WndProc方法. unit Unit1; interface uses Windows, Messages, SysUtils ...

  5. nginx 配置多个主机

    <pre name="code" class="html"> server { listen 8001; server_name localhost ...

  6. Sudoku(回溯)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12075   Accepted: 6026   Special Judge ...

  7. COJ 0046 20701除法

    20701除法 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有满足表达式abcd ...

  8. phpMyAdmin <= 4.0.4.1 import.php GLOBALS变量注入漏洞

    漏洞版本: phpMyAdmin <= 4.0.4.1 漏洞描述: CVE(CAN) ID: CVE-2013-4729 phpmyadmin是MySQL数据库的在线管理工具,主要功能包括在线创 ...

  9. MVC传值方式及优缺点

    说在前面文章转自 http://www.cxyclub.cn/n/49493/ 在MVC控件器传递多个Model到视图,使用ViewData,ViewBag,部分视图,TempData,ViewMod ...

  10. Hat's Fibonacci(大数问题)

    #include <iostream>#include <stdio.h>#include <string.h>using namespace std;int a[ ...