Word复制和替换实例
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复制和替换实例的更多相关文章
- [No000051]如何去掉word复制过来的文字背景色?
我们经常从网上copy一些自己需要的材料到word里.不过常常会把别人的背景色一起拷贝过来.那么如何去掉word复制过来的文字背景色? 方法/步骤 第一步选ctrl+A(全选),找到页面布局→页面边框 ...
- ZeroclipboardJS+flash实现将内容复制到剪贴板实例
Zeroclipboard 的实现原理 Zeroclipboard 利用 Flash 进行复制,之前有 Clipboard Copy 解决方案,其利用的是一个隐藏的 Flash.但最新的 Flash ...
- 烂泥:【解决】word复制windows live writer没有图片
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 在使用windows live writer发表博客,博客先是在是word2013中进行编辑,编辑完毕后我会复制到windows live writer ...
- Word中批量替换软回车
在平时工作中,有时候需要拷贝一些截取自网页上的文字,当选中后拷贝到Word中时,有时候在每行的结尾出现如下的符号,,这给后期文字的整理带来了很多不便,在此记录从网上获取的解决方法,以免遗忘和便于查找. ...
- jquery插入,复制、替换和删除节点
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Python正则表达式如何进行字符串替换实例
Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...
- Python之word文档替换字符串(也可以用于短模板套用)
Python之word文档替换字符串(也可以用于短模板套用),代码如下: 1 ''' 2 #word模板套用1:创建模板,适合比较短的文档 3 ''' 4 5 #导入所需库 6 from docx i ...
- c#调用word com组件 替换书签套打
安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...
- 富文本框编辑器实现:a、支持图片复制粘贴;b、支持word复制粘贴图文。
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
随机推荐
- [BZOJ 1207] [HNOI 2004] 打鼹鼠 【DP】
题目链接:BZOJ - 1207 题目分析 每一次打鼹鼠一定是从上一次打某只鼹鼠转移过来的,从打第 j 只鼹鼠能不能转移到打第 i 只鼹鼠,算一下曼哈顿距离和时间差就知道了. 那么就有一个 DP ,用 ...
- Best Pratices——Make the Web Faster
https://developers.google.com/speed/ PageSpeed Rules Optimizing caching — keeping your application's ...
- 如何给Qlabel添加clicked属性(覆盖mousePressEvent,处理QMouseEvent消息,反正是软绘制,想怎么样就怎么样)
clickedLabel.h #ifndef CLICKLABEL_H #define CLICKLABEL_H #include <QLabel> #include <QMouse ...
- RPM常见用法
rpm常见的用法: 命令 说明 rpm -i <.rpm file name> 安装指定的 .rpm 文件 rpm -U <.rpm file name> 用指定的.rpm文件 ...
- UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]
题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...
- 【转】Android JNI编程—JNI基础
原文网址:http://www.jianshu.com/p/aba734d5b5cd 最近看到了很多关于热补的开源项目——Depoxed(阿里).AnFix(阿里).DynamicAPK(携程)等,它 ...
- 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸
410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:pvz.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] Plants vs ...
- sharepoint2007的中文版
今天终于下载到了sharepoint2007的中文版了,以前微软的测试板也没有中文版,今天终于下载到了,经过测试,用英文版的sn也是可以用的.微软提供的测试版,输入正式的key,就可以变成正式版了.我 ...
- android导入项目出错处理
问题: 执行Import-Android下的Existing Android Code Into Workspace 解决方法:
- Fragmen横竖屏切换,导致页面混乱,oncreateView重复调用
在清单文件Activity设置属性 android:screenOrientation="landscape" android:configChanges="screen ...