2013-10-08 20 views
6

Tôi có một số mã để thay thế văn bản bên trong một từ docx 2010.C# interop tìm và thay thế mọi thứ

 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); 

Tính năng này không có định dạng. Nhưng khi {id} được định dạng, nó không thay thế văn bản.

Tôi làm cách nào để mã này bỏ qua định dạng?

Trả lời

21

Tôi sử dụng chức năng này để tìm và thay thế. bạn có thể chỉ định bất kỳ tùy chọn nào.

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);     
} 

Và sử dụng sẽ là:

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"); 

Và bạn có thể sử dụng chức năng FindAndReplace hơn và hơn ....
Hope this helps.

+0

Wow! Tuyệt vời, cảm ơn bạn! – Lukas

+0

Vui lòng trợ giúp :) – joecop

+1

Điều này không phù hợp. Đôi khi nó hoạt động, đôi khi nó không. –

0

Bạn có thể thử này:

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

nó không hoạt động với tôi –

0

Từ Visual Studio 2013 bạn có thể làm điều này:

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

(văn vì lợi ích của bất cứ ai, như tôi, người tình cờ gặp câu hỏi này nhưng là không nhất thiết phải sử dụng cùng một phiên bản của các công cụ như OP.)

+0

Word nằm trong không gian tên sau: Microsoft.Office.Interop – R2D2

0

Phương thức phân chia chuỗi nếu chuỗi chứa hơn 255 ký tự.

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); 
    } 
Các vấn đề liên quan