http://blog.csdn.net/wwei466/article/details/6578120
現在的項目有個功能是要替換掉word文檔中的空格,搜尋了半天得到一個方法是在word內全域替換^p^p為^p,這樣就可以消除一遍空格,當然如果有連續的空格時,需要連續替換幾次就可以了。
現在有了方法就好多了,那麼看c#word作業碼如何寫了。
使用c# 進行word操作替換操作時需要使用com方法,Find.Excute,這個也好說,搜尋一下就可以了。
網上代碼如下:
[c-sharp]
view plaincopy
- Word.Find myFind = mySelection.Find;
-
- Console.WriteLine(myFind.Text);
- object findText = "alow";
- object replaceText ="allow";
-
- // Find "alow" and replace with "allow"
- try
- {
- myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
- ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);
-
- }
但是,試了很久就是不成功。總是報錯,程式一到這裡就卡住了。
差了半天好像是因為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
- object myFind = mySelection.Find;
-
- Console.WriteLine(myFind.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, myFind, null));
- object findText = "alow";
- object replaceText ="allow";
-
- // Find "alow" and replace with "allow"
- try
- {
- object[] Parameters;
- Parameters = new object[15];
- Parameters[0] = findText;
- Parameters[1] = missing;
- Parameters[2] = missing;
- Parameters[3] = missing;
- Parameters[4] = missing;
- Parameters[5] = missing;
- Parameters[6] = missing;
- Parameters[7] = missing;
- Parameters[8] = missing;
- Parameters[9] = replaceText;
- Parameters[10] = missing;
- Parameters[11] = missing;
- Parameters[12] = missing;
- Parameters[13] = missing;
- Parameters[14] = missing;
-
- myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters );
- }
結果 程式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
- Word.Find myFind = mySelection.Find;
-
- Console.WriteLine(myFind.Text);
- object findText = "alow";
- object replaceText ="allow";
-
- // Find "alow" and replace with "allow"
- try
- {
- myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
- ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);
-
- }
但是,試了很久就是不成功。總是報錯,程式一到這裡就卡住了。
差了半天好像是因為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
- object myFind = mySelection.Find;
-
- Console.WriteLine(myFind.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, myFind, null));
- object findText = "alow";
- object replaceText ="allow";
-
- // Find "alow" and replace with "allow"
- try
- {
- object[] Parameters;
- Parameters = new object[15];
- Parameters[0] = findText;
- Parameters[1] = missing;
- Parameters[2] = missing;
- Parameters[3] = missing;
- Parameters[4] = missing;
- Parameters[5] = missing;
- Parameters[6] = missing;
- Parameters[7] = missing;
- Parameters[8] = missing;
- Parameters[9] = replaceText;
- Parameters[10] = missing;
- Parameters[11] = missing;
- Parameters[12] = missing;
- Parameters[13] = missing;
- Parameters[14] = missing;
-
- myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters );
- }
結果 程式Ok。。。寫在這裡標記一下。
另外:好像中文的就沒幾個問這個問題的。不知道是我系統不行,碰到了這個問題,還是大家都沒遇到過。。。
總之,我貢獻一個中文的解決方案吧。
原文地址是:http://support.microsoft.com/default.aspx?scid=kb;en-us;313104