2012-05-04 130 views
1

我想使用C#和Interop在Microsft Word文檔中用正常段落標記代碼^ p替換段落標記(^ 13)的所有替代代碼。您可以使用Interop在MS Word中用^ p段落標記替換^ 13段落標記嗎?

我一直在使用Microsoft.Office.Interop.Word.Selection.Find.Execute()方法。

例如..

.Application.ActiveWindow.Selection.Find.Execute(
       ref findText, 
       ref matchCase, 
       ref matchWholeWord, 
       ref matchWildcards, 
       ref matchSoundsLike, 
       ref matchAllWordForms, 
       ref findForward, 
       ref findWrap, 
       ref findFormat, 
       ref replaceText, 
       ref replaceAll, 
       ref missing, 
       ref missing, 
       ref missing, 
       ref missing); 
  • FINDTEXT = 「^ 13」
  • matchCase =真
  • matchWholeWord =真
  • matchWildcards =真
  • matchSoundsLike =假
  • matchAllWordForms = false
  • findForward =真
  • findWrap = WdFindWrap.wdFindContinue
  • findFormat =假
  • replaceText = 「^ P」
  • 的replaceAll = WdReplace.wdReplaceAll

使用上面的代碼中,^ 13標記不被^ p標記替換。

有誰知道我該如何糾正這個問題?

+1

的風格作爲值replaceText然後上面的代碼工作 –

回答

1

檢查下面我代碼:

// Create the Word application and declare a document 
      Word.Application word = new Word.Application(); 
      Word.Document doc = new Word.Document(); 

      // Define an object to pass to the API for missing parameters 
      object missing = System.Type.Missing; 

      try 
      { 
       // Everything that goes to the interop must be an object 
       object fileName = @"D:\test.docx"; 

       // Open the Word document. 
       // Pass the "missing" object defined above to all optional 
       // parameters. All parameters must be of type object, 
       // and passed by reference. 
       doc = word.Documents.Open(ref fileName, 
        ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing); 

       // Activate the document 
       doc.Activate(); 

       // Loop through the StoryRanges (sections of the Word doc) 
       foreach (Word.Range tmpRange in doc.StoryRanges) 
       { 
        // Set the text to find and replace 
        tmpRange.Find.Text = "xml"; 
        tmpRange.Find.Replacement.Text = "XML"; 

        // Set the Find.Wrap property to continue (so it doesn't 
        // prompt the user or stop when it hits the end of 
        // the section) 
        tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue; 

        // Declare an object to pass as a parameter that sets 
        // the Replace parameter to the "wdReplaceAll" enum 
        object replaceAll = Word.WdReplace.wdReplaceAll; 

        // Execute the Find and Replace -- notice that the 
        // 11th parameter is the "replaceAll" enum object 
        tmpRange.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); 
       } 

       // Save the changes 
       doc.Save(); 

       // Close the doc and exit the app 
       doc.Close(ref missing, ref missing, ref missing); 
       word.Application.Quit(ref missing, ref missing, ref missing); 
      } 
      catch (Exception ex) 
      { 
       doc.Close(ref missing, ref missing, ref missing); 
       word.Application.Quit(ref missing, ref missing, ref missing); 
       System.Diagnostics.Process.Start("D:\\test.docx"); 
      } 

一兩件事:這裏注意:使用Word =的Microsoft.Office.Interop.Word;

+0

我試過你提供的代碼,但它沒有工作。它似乎與我提供的代碼非常相似,但爲每個StoryRange調用Find方法,而不是整個文檔。您提供的鏈接是我不想付費的第三方解決方案。不過謝謝。 –

+1

我發現當我嘗試替換正確的字符時,我和您的代碼都起作用。看起來^ 13在VBA中相當於Chr(13),而在.NET中它又相當於\ r。所以我需要用^ p替換\ r並且它工作 –

0

,如果我不要誤會,你不能調用查找和替換段落,但是當我用\ r(而不是^ 13)作爲FINDTEXT和^ P值,你可以改變它們paragraph