【问题标题】:Can you replace ^13 paragraph markers with ^p paragraph markers in MS Word using Interop?您可以使用 Interop 在 MS Word 中将 ^13 段落标记替换为 ^p 段落标记吗?
【发布时间】:2012-05-04 06:55:44
【问题描述】:

我想使用 C# 和互操作,用 Microsft 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 = true
  • matchWholeWord = true
  • matchWildcards = true
  • matchSoundsLike = 假
  • matchAllWordForms = 假
  • findForward = true
  • findWrap = WdFindWrap.wdFindContinue
  • findFormat = 假
  • replaceText = "^p"
  • replaceAll = WdReplace.wdReplaceAll

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

有谁知道我该如何解决这个问题?

【问题讨论】:

  • 当我使用 \r (而不是 ^13) 作为 findText 的值和 ^p 作为值 replaceText 然后上面的代码工作

标签: c# ms-word interop office-interop


【解决方案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;

【讨论】:

  • 我尝试了您提供的代码,但没有成功。它似乎与我提供的代码非常相似,但它为每个 StoryRange 调用 Find 方法,而不是为整个文档调用。您提供的链接是我不想支付的第 3 方解决方案。不过谢谢。
  • 当我尝试替换正确的字符时,我发现我的代码和你的代码都有效。似乎 ^13 在 VBA 中等效于 Chr(13),而 Chr(13) 又等效于 .NET 中的 \r。所以我需要用 ^p 替换 \r 并且它起作用了
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多