【问题标题】:Find all indented text查找所有缩进文本
【发布时间】:2019-02-28 09:47:31
【问题描述】:

我想查找缩进 8 个空格的文档中的所有文本。下面的代码应该找到这些行并选择该行以供以后处理。但是,代码无条件地查找/选择所有行。出了什么问题?

我必须在每次匹配后重置查找结果吗?怎么做的?

Sub findAndSelectIntendedlines()
    Dim matched As Boolean
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Forward = True
        .Execute findtext:="^p        ", Forward:=True, Format:=False, Wrap:=wdFindStop
        matched = .Found
     End With
    If matched Then
            Selection.MoveDown Unit:=wdLine, Count:=1
            Selection.Find.Parent.Expand Unit:=wdLine
            Selection.Find.Parent.Copy
            'Debug.Print Selection
    Else
         Debug.Print "not matched"
    End If
End Sub

【问题讨论】:

  • 新段落不会被第一行标尺边距“缩进”还是更简单地使用一个或多个制表符?使用8个空格开始新段落的.docx的来源是什么?
  • 输入是纯文本文件。改变问题/问题通常不是解决方案/答案?
  • 那我就把它当作一个文本文件。您可以将其作为流打开并查找 vbCRLF & Space(8)。
  • 仍然是改变问题,也就是为不同/改变的问题提供解决方案。我将纯文本导入word。随后,日志文本将由单独的颜色格式化。这个丰富的wordtext随后将在word中使用,并将保存为DOCX。我正在 DOCX word 文档中寻找一个非常基本的 vba 解决方案。

标签: vba ms-word text-manipulation


【解决方案1】:

我已经修改了一些用于替换空格的代码。该例程查找 2 个或更多空格。我添加了一些代码并注释掉了几行,以便您测试它是否符合您的要求。

Sub FindAndReplaceEmptySpaces()
    Dim rng As Word.Range
    Set rng = ActiveDocument.Content
    With rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = " {2,}" 'look for 2 or more
        .Replacement.Text = " " 'replace with 1
        .Forward = True
        .Wrap = Word.WdFindWrap.wdFindStop
        .Format = False
        .MatchWildcards = True
        'The following was added to demo the search
        .Execute
        Do While .found
            rng.MoveEnd Word.WdUnits.wdParagraph, Count:=1
            Debug.Print rng.Text
            rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            .Execute
        Loop
        'end of demo code
    End With
'The following code is commented out. Remove comments and demo code above
'to return to the automatically removing extra blank spaces
'    rng.Find.Execute Replace:=Word.WdReplace.wdReplaceAll
'    rng.Find.ClearFormatting
'    rng.Find.Replacement.ClearFormatting

End Sub

【讨论】:

  • 我收到 使用您的代码。
  • @olippuner 尝试用分号替换逗号 - 您可能正在使用分号作为列表分隔符的系统...
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 2012-02-28
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 2013-10-23
相关资源
最近更新 更多