您可以使用互操作将 ^13 段落标记替换为 MS Word 中的 ^p 段落标记吗?

本文关键字:段落标 Word MS 中的 替换 可以使 互操作 | 更新日期: 2023-09-27 18:33:13

我想使用C#和互操作将Microsft Word文档中段落标记(^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);
  • 查找文本 = "^13"
  • 匹配大小写 = 真
  • 匹配全字 = 真
  • 匹配通配符 = 真
  • 匹配听起来像 = 假
  • matchAllWordForms = false
  • 查找转发 = 真
  • findWrap = WdFindWrap.wdFindContinue
  • 查找格式 = 假
  • 替换文本 = "^p"
  • replaceAll = WdReplace.wdReplaceAll

使用上面的代码,^13 标记不会被 ^p 标记替换。

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

您可以使用互操作将 ^13 段落标记替换为 MS Word 中的 ^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;

如果我

没记错的话,你不能为段落调用查找和替换,但你可以改变它们的段落风格