现在的位置: 首页 > 综合 > 正文

c# word替换(Find.Excute方法)操作 去除空行方法

2013年08月26日 ⁄ 综合 ⁄ 共 4110字 ⁄ 字号 评论关闭

http://blog.csdn.net/wwei466/article/details/6578120

现在的项目有个功能是要替换掉word文档中的空格,搜索了半天得到一个方法是在word内全局替换^p^p为^p,这样就可以消除一遍空格,当然如果有连续的空格时,需要连续替换几次就可以了。

现在有了方法就好多了,那么看c#word操作代码如何写了。

使用c# 进行word操作替换操作时需要使用com方法,Find.Excute,这个也好说,搜索一下就可以了。

网上代码如下:

  1. Word.Find myFind = mySelection.Find;  
  2.   
  3. Console.WriteLine(myFind.Text);  
  4. object findText = "alow";  
  5. object replaceText ="allow";  
  6.   
  7. // Find "alow" and replace with "allow"  
  8. try  
  9. {  
  10.    myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,  
  11. ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);  
  12.   
  13. }  

但是,试了很久就是不成功。总是报错,程序一到这里就卡住了。

差了半天好像是因为word重新使用了Excel95用过的GUIDs,如果你的word 动态链接库 注册在Excel95 动态链接库之前,你就会出现这个问题。原文是:

Word reuses Globally Unique Identifiers (GUIDs) that Microsoft Excel 95 originally used. If the Excel 95 type library is registered after the Word type library, you will experience this problem.

这就话我不太理解,怎么跟Excel95的动态链接库联系上了。。。现在都2011了!!

不过这篇文章是06年写的。权当是微软使用了以前的动态链接库吧(微软的软件架构也这么烂。。。O(∩_∩)O~)

 

好了,解决的方式是 使用反射调去com方法。代码如下

 

  1. object myFind = mySelection.Find;  
  2.   
  3. Console.WriteLine(myFind.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, myFind, null));  
  4. object findText = "alow";  
  5. object replaceText ="allow";  
  6.   
  7. // Find "alow" and replace with "allow"  
  8. try  
  9. {  
  10.    object[] Parameters;  
  11.    Parameters = new object[15];  
  12.    Parameters[0] = findText;  
  13.    Parameters[1] = missing;  
  14.    Parameters[2] = missing;  
  15.    Parameters[3] = missing;  
  16.    Parameters[4] = missing;  
  17.    Parameters[5] = missing;  
  18.    Parameters[6] = missing;  
  19.    Parameters[7] = missing;  
  20.    Parameters[8] = missing;  
  21.    Parameters[9] = replaceText;  
  22.    Parameters[10] = missing;  
  23.    Parameters[11] = missing;  
  24.    Parameters[12] = missing;  
  25.    Parameters[13] = missing;  
  26.    Parameters[14] = missing;  
  27.   
  28.    myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters );  
  29. }  

 

结果 程序Ok。。。写在这里标记一下。

另外:好像中文的就没几个问这个问题的。不知道是我系统不行,碰到了这个问题,还是大家都没遇到过。。。

总之,我贡献一个中文的解决方法吧。

 

 

原文地址是:http://support.microsoft.com/default.aspx?scid=kb;en-us;313104

现在的项目有个功能是要替换掉word文档中的空格,搜索了半天得到一个方法是在word内全局替换^p^p为^p,这样就可以消除一遍空格,当然如果有连续的空格时,需要连续替换几次就可以了。

现在有了方法就好多了,那么看c#word操作代码如何写了。

使用c# 进行word操作替换操作时需要使用com方法,Find.Excute,这个也好说,搜索一下就可以了。

网上代码如下:

  1. Word.Find myFind = mySelection.Find;  
  2.   
  3. Console.WriteLine(myFind.Text);  
  4. object findText = "alow";  
  5. object replaceText ="allow";  
  6.   
  7. // Find "alow" and replace with "allow"  
  8. try  
  9. {  
  10.    myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,  
  11. ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);  
  12.   
  13. }  

但是,试了很久就是不成功。总是报错,程序一到这里就卡住了。

差了半天好像是因为word重新使用了Excel95用过的GUIDs,如果你的word 动态链接库 注册在Excel95 动态链接库之前,你就会出现这个问题。原文是:

Word reuses Globally Unique Identifiers (GUIDs) that Microsoft Excel 95 originally used. If the Excel 95 type library is registered after the Word type library, you will experience this problem.

这就话我不太理解,怎么跟Excel95的动态链接库联系上了。。。现在都2011了!!

不过这篇文章是06年写的。权当是微软使用了以前的动态链接库吧(微软的软件架构也这么烂。。。O(∩_∩)O~)

 

好了,解决的方式是 使用反射调去com方法。代码如下

 

  1. object myFind = mySelection.Find;  
  2.   
  3. Console.WriteLine(myFind.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, myFind, null));  
  4. object findText = "alow";  
  5. object replaceText ="allow";  
  6.   
  7. // Find "alow" and replace with "allow"  
  8. try  
  9. {  
  10.    object[] Parameters;  
  11.    Parameters = new object[15];  
  12.    Parameters[0] = findText;  
  13.    Parameters[1] = missing;  
  14.    Parameters[2] = missing;  
  15.    Parameters[3] = missing;  
  16.    Parameters[4] = missing;  
  17.    Parameters[5] = missing;  
  18.    Parameters[6] = missing;  
  19.    Parameters[7] = missing;  
  20.    Parameters[8] = missing;  
  21.    Parameters[9] = replaceText;  
  22.    Parameters[10] = missing;  
  23.    Parameters[11] = missing;  
  24.    Parameters[12] = missing;  
  25.    Parameters[13] = missing;  
  26.    Parameters[14] = missing;  
  27.   
  28.    myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters );  
  29. }  

 

结果 程序Ok。。。写在这里标记一下。

另外:好像中文的就没几个问这个问题的。不知道是我系统不行,碰到了这个问题,还是大家都没遇到过。。。

总之,我贡献一个中文的解决方法吧。

 

 

原文地址是:http://support.microsoft.com/default.aspx?scid=kb;en-us;313104

抱歉!评论已关闭.