2013-10-08 55 views
6

我有一些代碼可以替換單詞2010 docx中的文本。c#word interop查找並替換所有內容

 object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx"); 

     Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true }; 

     Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true); 

     aDoc.Activate(); 

     Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find; 

     fnd.ClearFormatting(); 
     fnd.Replacement.ClearFormatting(); 
     fnd.Forward = true; 

     fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; 

     fnd.Text = "{id}"; 
     fnd.Replacement.Text = "123456"; 
     fnd.Execute(Replace: WdReplace.wdReplaceAll); 

這種方式沒有格式化。但是,{id}​​格式化後,它不會替換文本。

如何讓此代碼忽略格式?

回答

21

我用這個函數查找和替換。你可以指定任何選項。

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText) 
{ 
    //options 
    object matchCase = false; 
    object matchWholeWord = true; 
    object matchWildCards = false; 
    object matchSoundsLike = false; 
    object matchAllWordForms = false; 
    object forward = true; 
    object format = false; 
    object matchKashida = false; 
    object matchDiacritics = false; 
    object matchAlefHamza = false; 
    object matchControl = false; 
    object read_only = false; 
    object visible = true; 
    object replace = 2; 
    object wrap = 1; 
    //execute find and replace 
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, 
     ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace, 
     ref matchKashida ,ref matchDiacritics, ref matchAlefHamza, ref matchControl);     
} 

而且用法是:

object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx"); 
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true }; 
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true); 
aDoc.Activate(); 
FindAndReplace(wordApp, "{id}", "12345"); 

而且可以反覆使用的FindAndReplace功能....
希望這有助於。

+0

哇!很棒,謝謝! – Lukas

+0

樂意幫忙:) – joecop

+1

這是不一致的。有時它起作用,有時它不起作用。 –

0

你可以試試這個:

var doc = new Microsoft.Office.Interop.Word.Application().Documents.Open("document.docx"); 

doc.Content.Find.Execute("{id}", false, true, false, false, false, true, 1, false, "12345", 2, 
false, false, false, false); 
doc.Save(); 
+0

它不適用於我 –

0

在Visual Studio 2013,你可以這樣做:

Word.Range range = this.Application.ActiveDocument.Content; 
range.Find.ClearFormatting(); 
range.Find.Execute(FindText: "find text", ReplaceWith: "replace text", Replace: Word.WdReplace.wdReplaceAll); 

(發佈於任何人的利益,像我一樣,誰遇到了這個問題,但爲不一定使用與OP相同版本的工具。)

+0

Word位於以下命名空間中:Microsoft.Office.Interop – R2D2

0

如果字符串包含的字符數超過255個,則會將字符串除法。

void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, string findText, string replaceWithText) 
    { 
     if (replaceWithText.Length > 255) 
     { 
      FindAndReplace(doc, findText, findText + replaceWithText.Substring(255)); 
      replaceWithText = replaceWithText.Substring(0, 255); 
     } 

     //options 
     object matchCase = false; 
     object matchWholeWord = true; 
     object matchWildCards = false; 
     object matchSoundsLike = false; 
     object matchAllWordForms = false; 
     object forward = true; 
     object format = false; 
     object matchKashida = false; 
     object matchDiacritics = false; 
     object matchAlefHamza = false; 
     object matchControl = false; 
     object read_only = false; 
     object visible = true; 
     object replace = 2; 
     object wrap = 1; 

     //execute find and replace 
     doc.Selection.Find.Execute(findText, ref matchCase, ref matchWholeWord, 
      ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, replaceWithText, ref replace, 
      ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl); 
    }