C# 能否使用Interop将MS Word中的^13个段落标记替换为^p个段落标记?

C# 能否使用Interop将MS Word中的^13个段落标记替换为^p个段落标记?,c#,ms-word,interop,office-interop,C#,Ms Word,Interop,Office Interop,我想在Microsft Word文档中,使用C#和Interop将段落标记(^13)的所有替换代码替换为正常的段落标记代码^p 我一直在使用Microsoft.Office.Interop.Word.Selection.Find.Execute()方法 例如 .Application.ActiveWindow.Selection.Find.Execute( ref findText, ref matchCase,

我想在Microsft Word文档中,使用C#和Interop将段落标记(^13)的所有替换代码替换为正常的段落标记代码^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=true
  • matchWholeWord=true
  • 匹配通配符=true
  • matchSoundsLike=false
  • matchAllWordForms=false
  • findForward=true
  • findWrap=WdFindWrap.wdFindContinue
  • findFormat=false
  • replaceText=“^p”
  • replaceAll=WdReplace.wdReplaceAll
使用上述代码,^13标记不会被^p标记替换

有人知道我该如何纠正这个问题吗?

请检查下面的代码:

 // 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

检查下面的我的代码:

 // 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

如果我没有弄错,你不能为段落调用“查找和替换”,但你可以更改它们的样式

如果我没有弄错,你不能为段落调用“查找和替换”,但你可以更改它们的样式

我尝试了你提供的代码,但它不起作用。它似乎与我提供的代码非常相似,但是为每个故事范围而不是整个文档调用Find方法。你提供的链接是我不想付费的第三方解决方案。不过,谢谢。当我尝试替换正确的字符时,我发现我的代码和你的代码都起作用。似乎^13在VBA中等同于Chr(13),而Chr(13)又相当于.NET中的\r。因此,我需要将\r替换为^p,它工作了。我尝试了您提供的代码,但没有工作。它似乎与我提供的代码非常相似,但是为每个故事范围而不是整个文档调用Find方法。你提供的链接是我不想付费的第三方解决方案。不过,谢谢。当我尝试替换正确的字符时,我发现我的代码和你的代码都起作用。似乎^13在VBA中等同于Chr(13),而Chr(13)又相当于.NET中的\r。因此,我需要用^p替换\r,当我使用\r(而不是^13)作为findText的值,使用^p作为value replaceText时,它起作用了。然后,上面的代码起作用了。当我使用\r(而不是^13)作为findText的值,使用^p作为value replaceText时,上面的代码起作用了