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

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - 可乐加冰

推荐一个单元测试模拟框架: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