惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
雷峰网
雷峰网
T
Tor Project blog
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
J
Java Code Geeks
AWS News Blog
AWS News Blog
Security Latest
Security Latest
Spread Privacy
Spread Privacy
T
Threatpost
博客园 - 三生石上(FineUI控件)
I
Intezer
G
Google Developers Blog
Scott Helme
Scott Helme
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
A
Arctic Wolf
F
Full Disclosure
P
Proofpoint News Feed
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
B
Blog
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
Know Your Adversary
Know Your Adversary

博客园 - 可乐加冰

推荐一个单元测试模拟框架:Nsubstitute QuickFIX/N与QuickFIX的.NET封装不同之处 QuickFIX/N入门:五、如何自定义FIX QuickFIX/N入门:四、使用消息循环分组 QuickFIX/N入门:三、 如何配置QuickFIX/N QuickFIX/N入门:二、发送消息及接收消息 QuickFIX/N入门:一、如何创建一个QuickFIX/N的应用程序 转、分享:PMP学习资料、考试资料推荐:第四版-2008版-吴永达 关于“类型初始值设定项引发异常” 浪费时间的主观原因 关于C#调用VC++.net程序集出现0x800736B1的异常 关于微软发布的Microsoft 图表控件 C#后期绑定方式来调用COM对象 Windows Mobile开发资源相关下载收录 值得珍藏的五十句话 Fund Managers Switching to Algorithmic Trading[转] winform窗体中嵌入显示Excel文件 让CheckBoxList,CheckBox 控件不能操作 - 可乐加冰 - 博客园 关于世界杯
C#在word文档中替换字符串
可乐加冰 · 2006-04-05 · via 博客园 - 可乐加冰

      在文档中搜索和替换字符串,先在word文档中标记字符串,然后再搜索标记字符串并用新的字符串替换标记字符串.主要是先选择整个文档,然后使用Find的Execute方法查找指定字符串并替换为相应字符串.

以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。

     ///<summary>
        
/// 在word 中查找一个字符串直接替换所需要的文本
        
/// </summary>
        
/// <param name="strOldText">原文本</param>
        
/// <param name="strNewText">新文本</param>
        
/// <returns></returns>

        public bool Replace(string strOldText,string strNewText)
        
{
            
this.oDoc.Content.Find.Text = strOldText ;
            
object FindText,  ReplaceWith, Replace ;// 
            object MissingValue = Type.Missing; 
            FindText 
= strOldText ;//要查找的文本
            ReplaceWith = strNewText ;//替换文本
               Replace = Word.WdReplace.wdReplaceAll ;/*wdReplaceAll - 替换找到的所有项。
                                                      * wdReplaceNone - 不替换找到的任何项。
                                                    * wdReplaceOne - 替换找到的第一项。
                                                    * 
*/

            
this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
            if (this.oDoc.Content.Find.Execute(
                
ref FindText,ref MissingValue,
                
ref MissingValue,ref MissingValue,
                
ref MissingValue,ref MissingValue,
                
ref MissingValue,ref MissingValue,ref MissingValue,
                
ref ReplaceWith,ref Replace,
                
ref MissingValue,ref MissingValue,
                
ref MissingValue,ref MissingValue))
            
{
                
return true ;
            }

            
return false ;
            
        }

说明:其中oDoc是一个word文档的Document对象.

此外还可以 运用Word Application 对象Selection的Find.

public bool SearchReplace(string strOldText,string strNewText)
        

            
object replaceAll = Word.WdReplace.wdReplaceAll; 
            
object missing = Type.Missing; 
            
                //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
            
this.oWordApplic.Selection.Find.ClearFormatting(); 
            oWordApplic.Selection.Find.Text 
= strOldText; 

            oWordApplic.Selection.Find.Replacement.ClearFormatting(); 
            oWordApplic.Selection.Find.Replacement.Text 
= strNewText; 

            
if (oWordApplic.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))
            
{
                
return true ;
            }

            
return false ;
        }

    注:oWordApplic是一个Word Application 对象

   Find.Execute 方法详细介绍请看文档:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-us,VS.80).aspx
  
当然也可以使用word文档的书签BookMark.使用 Bookmark 的 Range 属性可将文本插入占位符书签,以便能够在以后检索文本,或替换已包含文本的书签中的文本。可以参考http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/dv_wrcore/html/wrtskhowtoupdatebookmarktext.asp