Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12877445
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2014-03-15 16:49:51

       想用C#动态更换WORD文档中的一些文本,如果用了一些不当的方法,很可能会出现有某PC上能正常替换在另外一台PC则不能替换的问题。
      比如下面注释掉的代码就会出现问题。


  1.         ///
  2.         /// personalName()用来调用替换名字的
  3.         ///
  4.         /// 要被替换的名字
  5.         /// 要替换的名字
  6.         public void replaceName(string oldName,string newName)
  7.         {
  8.             //Word.Find findObject = word.Selection.Find;
  9.             //findObject.ClearFormatting();
  10.             //findObject.Text = oldName;
  11.             //findObject.Replacement.ClearFormatting();
  12.             //findObject.Replacement.Font.Name = "黑体";
  13.             //findObject.Replacement.Text = newName;
  14.             //object replaceAll = Word.WdReplace.wdReplaceAll;
  15.             //findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  16.             // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  17.             // ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  18.             //object replaceAll = Word.WdReplace.wdReplaceAll;
  19.             //object missing = Type.Missing;
  20.             ////首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
  21.             //this.word.Selection.Find.ClearFormatting();
  22.             //word.Selection.Find.Text = oldName;
  23.             //word.Selection.Find.Replacement.ClearFormatting();
  24.             //word.Selection.Find.Replacement.Text = newName;
  25.             //word.Selection.Find.Replacement.Font.Name = "黑体";
  26.             //word.Selection.Find.Execute(
  27.             // ref missing, ref missing, ref missing, ref missing, ref missing,
  28.             // ref missing, ref missing, ref missing, ref missing, ref missing,
  29.             // ref replaceAll, ref missing, ref missing, ref missing, ref missing);
  30.             object myFind = word.Selection.Find;
  31.             object findText = oldName;
  32.             object replaceText = newName;
  33.             object replaceAll = Word.WdReplace.wdReplaceAll;
  34.             object oMissing = System.Reflection.Missing.Value;
  35.             try
  36.             {
  37.                 object[] Parameters;
  38.                 Parameters = new object[15];
  39.                 Parameters[0] = findText;
  40.                 Parameters[1] = oMissing;
  41.                 Parameters[2] = oMissing;
  42.                 Parameters[3] = oMissing;
  43.                 Parameters[4] = oMissing;
  44.                 Parameters[5] = oMissing;
  45.                 Parameters[6] = oMissing;
  46.                 Parameters[7] = oMissing;
  47.                 Parameters[8] = oMissing;
  48.                 Parameters[9] = replaceText;
  49.                 Parameters[10] = replaceAll;
  50.                 Parameters[11] = oMissing;
  51.                 Parameters[12] = oMissing;
  52.                 Parameters[13] = oMissing;
  53.                 Parameters[14] = oMissing;
  54.                 //word.Selection.Find.Replacement.Font.Name = "黑体";
  55.                 myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters);
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 MessageBox.Show(ex.ToString());
  60.             }
  61.             //object replaceAll = Word.WdReplace.wdReplaceAll;
  62.             //Word.Document document = word.ActiveDocument;
  63.             //Word.Range rng = document.Content;
  64.             //rng.Find.ClearFormatting();
  65.             //rng.Find.Text = oldName;
  66.             //rng.Find.Replacement.ClearFormatting();
  67.             //rng.Find.Replacement.Text = newName;
  68.             //rng.Find.Replacement.Font.Name = "黑体";
  69.             //rng.Find.Execute(
  70.             // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  71.             // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  72.             // ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  73.             // object missing = Type.Missing;
  74.             // object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
  75.             // Microsoft.Office.Interop.Word.Find findObject = myWord.ActiveDocument.Content.Find;
  76.             // findObject.ClearFormatting();
  77.             // findObject.Text = strOldText;
  78.             // Microsoft.Office.Interop.Word.Replacement replacementObject = findObject.Replacement;
  79.             // replacementObject.ClearFormatting();
  80.             // findObject.Replacement.Text = strNewText;
  81.             // findObject.Replacement.Font.Name = "黑体";
  82.             // findObject.Execute(ref missing, ref missing, ref missing,
  83.             // ref missing, ref missing, ref missing, ref missing,
  84.             // ref missing, ref missing, ref missing, ref replaceAll,
  85.             // ref missing, ref missing, ref missing, ref missing);
  86.         }



    以上四种方法,但是对我初学者看来都差不多,但是被注释的三种方法,在有些电脑上运行会提示错误:
************** 异常文本 **************
System.Runtime.InteropServices.SEHException: 外部组件发生异常。
   在 Microsoft.Office.Interop.Word.Find.set_Text(String prop)
   在 wordTest.Form2.replaceName(String oldName, String newName)
***************************************

image


    从网上搜集好多资料说把参数写开,就是上面没被注释的方法,就不再报“外部组件发生异常”的事情了。

 

转自博客:

http://blog.csdn.net/dragonzoebai/article/details/8689942

阅读(5473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~