/// <summary>
/// personalName()用來調用替換名字的
/// </summary>
/// <param name="oldName">要被替換的名字</param>
/// <param name="newName">要替換的名字</param>
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;
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)
***************************************
從網上搜集好多資料說把參數寫開,就是上面沒被注釋的方法,卻是再沒有報“外部組件發生異常”的事情,
但是想把字型設定成“黑體”不太會弄,總感覺//word.Selection.Find.Replacement.Font.Name = "黑體";這句話和上面其他語句很不協調,而且在我的電腦運行正常,但是在有的電腦直接崩掉,連錯誤提示都沒有,請教高人指點。