Word vba使用正则格式更改

发布于 2025-02-03 05:28:28 字数 1389 浏览 1 评论 0原文

我有一个具有SQL代码的Word文档。我有很棒的代码,发现我没有意识到我的文档中有很多不同的术语。我以140多个任期停了下来。我试图添加正则表达式,但我做错了。是否可以让它找到“启动”单词选择和从中的结尾词,然后将背景颜色更改为wdbrightgreen,以介绍这些术语之间的所有内容? 我没有把所有的田地都放在三个场上。有没有办法将ARRFIND搜索整个文档而不是特定术语?

Dim ArrFnd As Variant
Dim i As Long
Dim x As Long
Dim y As Long
Dim regexObject As RegExp

Dim strField1(1 To 200) As String
Dim strField2(1 To 20) As String
strField1(1) = "SELECT DISTINCT    policy_id"
strField1(2) = "SELECT DISTINCT  policy_id"
strField1(3) = "SELECT DISTINCT   P.policy_id"

For x = 1 To 150
    ArrFnd = Array(strField1(x))
    With ActiveDocument
        For i = 0 To UBound(ArrFnd)
            With .Range
                With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Format = False
                .Forward = True
                .Wrap = wdFindStop
                .MatchCase = True
                .MatchWholeWord = True
                        .Text = ArrFnd(i)
                    With regexObject
                         .Pattern = "[SELECT-FROM]"
                    End With
                .Replacement.Text = ""
            End With
                Do While .Find.Execute
                    .Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
                    .Collapse wdCollapseEnd
                    Loop
            End With
        Next i
    End With
Next x

I have a word document that has SQL code in it. I have great code that finds specific terms I didn't realize that I had so many different terms in my document. I stopped at over 140 terms. I tried to add regex but I did it wrong. Would it be possible to have it find the start word SELECT and the end word FROM and change the background color to wdBrightGreen for everything between those terms?
I did not put all the fields in I stopped at three. Is there a way to put the ArrFind to search the whole document instead of specific terms?

Dim ArrFnd As Variant
Dim i As Long
Dim x As Long
Dim y As Long
Dim regexObject As RegExp

Dim strField1(1 To 200) As String
Dim strField2(1 To 20) As String
strField1(1) = "SELECT DISTINCT    policy_id"
strField1(2) = "SELECT DISTINCT  policy_id"
strField1(3) = "SELECT DISTINCT   P.policy_id"

For x = 1 To 150
    ArrFnd = Array(strField1(x))
    With ActiveDocument
        For i = 0 To UBound(ArrFnd)
            With .Range
                With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Format = False
                .Forward = True
                .Wrap = wdFindStop
                .MatchCase = True
                .MatchWholeWord = True
                        .Text = ArrFnd(i)
                    With regexObject
                         .Pattern = "[SELECT-FROM]"
                    End With
                .Replacement.Text = ""
            End With
                Do While .Find.Execute
                    .Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
                    .Collapse wdCollapseEnd
                    Loop
            End With
        Next i
    End With
Next x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2025-02-10 05:28:28

一个人想知道为什么您会遵守代码中采用的方法,鉴于以下方式: MS Word vba无法选择特定的单词来更改背景颜色

您现在所描述的是什么

Sub HiliteWords()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Text = "SELECT[!^13^l]@FROM"
    .Replacement.Text = ""
  End With
  Do While .Find.Execute
    .Start = .Words.First.End
    .End = .Words.Last.Start - 1
    .Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub

无论 阴影,插入:

If .Words.First.Text = "DISTINCT " Then .Start = .Words.First.End

之前:

    .End = .Words.Last.Start - 1

One wonders why you persist with the approach taken in your code, in light of: MS Word VBA Can't select specific words to change background color

Regardless, for what you are now describing, all you need is:

Sub HiliteWords()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Text = "SELECT[!^13^l]@FROM"
    .Replacement.Text = ""
  End With
  Do While .Find.Execute
    .Start = .Words.First.End
    .End = .Words.Last.Start - 1
    .Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub

If you also don't want DISTINCT shaded, insert:

If .Words.First.Text = "DISTINCT " Then .Start = .Words.First.End

before:

    .End = .Words.Last.Start - 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文