如何将宏应用于所有选定的文本?

如何将宏应用于所有选定的文本?

一位前同事在处理要导入另一个程序的文档时为我们创建了一个自动换行文本宏。

我终于找到了他为我们创建的代码。目前它仅适用于 Word 文档中的每个部分。

Sub WrapText()

' WrapText Macro
Dim p As Variant
Dim i As Long

On Error Resume Next
'count the number of paragraphs
    p = Selection.Paragraphs.Count

'Position the cursor at the end of the first paragraph. MoveDown is used
'because it works with the wdParagraph unit.  Other commands only work with
'lines or sentences.
    Selection.Collapse (wdCollapseStart)
    Selection.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.Collapse (wdCollapseEnd)
    Selection.MoveLeft unit:=wdCharacter, Count:=1

'Begin loop.  p -1 prevents wrapping text from the paragraph following the selection
    For i = 1 To p - 1
        Selection.TypeText Text:=" ## "
        Selection.Delete unit:=wdCharacter, Count:=1
        Selection.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdExtend
        Selection.Collapse (wdCollapseEnd)
        Selection.MoveLeft unit:=wdCharacter, Count:=1
    Next i
End Sub

示例文档文本

这是包含标题和正文的文档的样子。(我还添加了我们试图替换的硬回车点)

1. SCOPE^p
    Summarizing, then, we assume that most of the 
    methodological work in modern linguistics is not quite 
    equivalent to the traditional practice of grammarians.^p

    Of course, the earlier discussion of deviance suffices to 
    account for nondistinctness in the sense of distinctive 
    feature theory.^p

    1.1 Heading2^p
        Summarizing, then, we assume that most of the 
        methodological work in modern linguistics is not quite 
        equivalent to the traditional practice of grammarians.^p

        Of course, the earlier discussion of deviance suffices to 
        account for nondistinctness in the sense of distinctive 
        feature theory.^p

现在运行宏会使上面的文本看起来像这样。

Running the macro the first time with the text selected below
1. SCOPE^p
    Summarizing, then, we assume that most of the 
    methodological work in modern linguistics is not quite 
    equivalent to the traditional practice of grammarians.##Of 
    course, the earlier discussion of deviance suffices to 
    account for nondistinctness in the sense of distinctive 
    feature theory.##

Running the macro a second time with the text selected below
    1.1 Heading2^p
        Summarizing, then, we assume that most of the 
        methodological work in modern linguistics is not quite 
        equivalent to the traditional practice of grammarians.##Of 
        course, the earlier discussion of deviance suffices to 
        account for nondistinctness in the sense of distinctive 
        feature theory.##

现在我必须在每个单独的部分上手动运行这个宏,它会用##替换它找到的任何硬回车(我们在另一个程序中使用##来处理导入后的数据)。

我尝试通过使用“样式:正文”替换所有 ^p 来解决问题,但它仍然会替换 Word 文档中除标题 1 样式之外的所有内容的段落标记。

我想知道是否有办法让此代码遍历 Word 文档中选定的所有文本,或者遍历 Word 文档中除标题/标题/表格之外的所有文本

或者是否有一种我不知道的更简单的方法可以做到这一点。

答案1

尝试检查以下段落是否具有相同的正文样式 - 如果没有,则跳过替换:

Sub WrapText()
Dim para as Paragraph
For Each para in ActiveDocument.Paragraphs
   para.Range.Select
   'if this is last para, no need to check
   If ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count = ActiveDocument.Paragraphs.Count Then Exit For
   If para.Style="Body Text" then
      If para.Next.Style="Body Text" then para.Range = Replace(para.Range, Chr(13), "##")
   End If
Next
End Sub

相关内容