2012-05-04 5 views
1

私は、C#とInteropを使用して、段落マーカー(^ 13)のすべての代替コードをMicrosft Word文書の通常の段落マーカーコード^ pに置き換えたいと考えています。Interopを使用して^ 13段落マーカーをMS Wordの^ p段落マーカーに置き換えることはできますか?

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と同じです。だから、\ rを^ pに置き換える必要があった。 –

0

私はFINDTEXTの値と^ pと\ rを(というよりも^ 13)を使用する場合paragraph

関連する問題