C#產生word尋找替換中的問題

來源:互聯網
上載者:User

        /// <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 = "黑體";這句話和上面其他語句很不協調,而且在我的電腦運行正常,但是在有的電腦直接崩掉,連錯誤提示都沒有,請教高人指點。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.