2013-10-08 7 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

あなたはこの試みることができます:あなたがこれを行うことができますのVisual Studio 2013から

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

を:

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

は(この質問に出くわしたが、誰であるか、私のように、誰の利益のために投稿

+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); 
    } 
関連する問題