想用C#动态更换WORD文档中的一些文本,如果用了一些不当的方法,很可能会出现有某PC上能正常替换在另外一台PC则不能替换的问题。
比如下面注释掉的代码就会出现问题。
- ///
- /// personalName()用来调用替换名字的
- ///
- /// 要被替换的名字
- /// 要替换的名字
- public void replaceName(string oldName,string newName)
- {
- //Word.Find findObject = word.Selection.Find;
- //findObject.ClearFormatting();
- //findObject.Text = oldName;
- //findObject.Replacement.ClearFormatting();
- //findObject.Replacement.Font.Name = "黑体";
- //findObject.Replacement.Text = newName;
- //object replaceAll = Word.WdReplace.wdReplaceAll;
- //findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- // ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
- //object replaceAll = Word.WdReplace.wdReplaceAll;
- //object missing = Type.Missing;
- ////首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
- //this.word.Selection.Find.ClearFormatting();
- //word.Selection.Find.Text = oldName;
- //word.Selection.Find.Replacement.ClearFormatting();
- //word.Selection.Find.Replacement.Text = newName;
- //word.Selection.Find.Replacement.Font.Name = "黑体";
- //word.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);
- object myFind = word.Selection.Find;
- object findText = oldName;
- object replaceText = newName;
- object replaceAll = Word.WdReplace.wdReplaceAll;
- object oMissing = System.Reflection.Missing.Value;
- try
- {
- object[] Parameters;
- Parameters = new object[15];
- Parameters[0] = findText;
- Parameters[1] = oMissing;
- Parameters[2] = oMissing;
- Parameters[3] = oMissing;
- Parameters[4] = oMissing;
- Parameters[5] = oMissing;
- Parameters[6] = oMissing;
- Parameters[7] = oMissing;
- Parameters[8] = oMissing;
- Parameters[9] = replaceText;
- Parameters[10] = replaceAll;
- Parameters[11] = oMissing;
- Parameters[12] = oMissing;
- Parameters[13] = oMissing;
- Parameters[14] = oMissing;
- //word.Selection.Find.Replacement.Font.Name = "黑体";
- myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- //object replaceAll = Word.WdReplace.wdReplaceAll;
- //Word.Document document = word.ActiveDocument;
- //Word.Range rng = document.Content;
- //rng.Find.ClearFormatting();
- //rng.Find.Text = oldName;
- //rng.Find.Replacement.ClearFormatting();
- //rng.Find.Replacement.Text = newName;
- //rng.Find.Replacement.Font.Name = "黑体";
- //rng.Find.Execute(
- // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- // ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
- // object missing = Type.Missing;
- // object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
- // Microsoft.Office.Interop.Word.Find findObject = myWord.ActiveDocument.Content.Find;
- // findObject.ClearFormatting();
- // findObject.Text = strOldText;
- // Microsoft.Office.Interop.Word.Replacement replacementObject = findObject.Replacement;
- // replacementObject.ClearFormatting();
- // findObject.Replacement.Text = strNewText;
- // findObject.Replacement.Font.Name = "黑体";
- // findObject.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);
- }
以上四种方法,但是对我初学者看来都差不多,但是被注释的三种方法,在有些电脑上运行会提示错误:
************** 异常文本 **************
System.Runtime.InteropServices.SEHException: 外部组件发生异常。
在 Microsoft.Office.Interop.Word.Find.set_Text(String prop)
在 wordTest.Form2.replaceName(String oldName, String newName)
***************************************
从网上搜集好多资料说把参数写开,就是上面没被注释的方法,就不再报“外部组件发生异常”的事情了。
转自博客: