【问题标题】:Word VBA - Find text between delimiters and convert to lower caseWord VBA - 在分隔符之间查找文本并转换为小写
【发布时间】:2010-12-03 12:02:04
【问题描述】:

我想查找 字符之间的文本,然后将任何找到的文本转换为“正常”大小写,其中单词的第一个字母大写。到目前为止,这是我所拥有的:

Function findTextBetweenCarots() As String

Dim strText As String

With Selection
    .Find.Text = "<"  ' what about <[^0-9]+>  ?
    .Find.Forward = True
    .Find.Wrap = wdFindContinue
End With

Selection.Find.Execute


 ' Application.Selection. ' how do I get the text between the other ">"?
    findCarotSymb = Application.Selection.Text

End Function

或者,有更好的方法吗?我还使用 VBScript Regex 5.5 库解决了这个问题,该库适用于简单文档,但不适用于具有复杂表格的某些文档。例如,尝试将文本加粗(为简单起见):

 Sub BoldUpperCaseWords()

  Dim regEx, Match, Matches
  Dim rngRange As Range

  Set regEx = New RegExp
  regEx.Pattern = "<[^0-9]+>"
  regEx.IgnoreCase = False
  regEx.Global = True

  Set Matches = regEx.Execute(ActiveDocument.Range.Text)

  For Each Match In Matches

     ActiveDocument.Range(Match.FirstIndex, Match.FirstIndex + Len(Match.Value)).Bold = True

  Next

End Sub

不适用于包含表格的文档。事实上,它甚至不会加粗正确的文本(&lt;&gt; 之间的文本。这让我相信我在这里缺少一个更广泛的问题。

【问题讨论】:

    标签: regex vba replace find


    【解决方案1】:

    我无法测试你的代码,但试试这个正则表达式

    regEx.Pattern = "<[^0-9<>]+>"
    

    您的正则表达式将匹配 "&lt;foo&gt;&lt;bar&gt;" 整个字符串。
    上面的正则表达式只会匹配&lt;foo&gt;,然后是&lt;bar&gt;等等。

    【讨论】:

    • 我得到了同样的结果。我正在使用的文件有一些特别之处。它们是 4 个列表,如果有帮助的话。我最多只能在下次编辑时提供示例文档的屏幕截图。
    • 打印 ActiveDocument.Range.Text 以查看它是否包含所有文本。还要检查regEx 属性是否处理多行文本。 (如果可能,尝试迭代表的行并将正则表达式应用于每一列)
    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2022-10-06
    • 2018-03-24
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多