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

來源:互聯網
上載者:User

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

現在的項目有個功能是要替換掉word文檔中的空格,搜尋了半天得到一個方法是在word內全域替換^p^p為^p,這樣就可以消除一遍空格,當然如果有連續的空格時,需要連續替換幾次就可以了。

現在有了方法就好多了,那麼看c#word作業碼如何寫了。

使用c# 進行word操作替換操作時需要使用com方法,Find.Excute,這個也好說,搜尋一下就可以了。

網上代碼如下:

[c-sharp]
view plaincopy
  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方法。代碼如下

 

[c-sharp]
view plaincopy
  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,這個也好說,搜尋一下就可以了。

網上代碼如下:

[c-sharp]
view plaincopy
  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方法。代碼如下

 

[c-sharp]
view plaincopy
  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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.