【问题标题】:vba ms-word find text and get adjacent numbervba ms-word 查找文本并获取相邻数字
【发布时间】:2017-11-05 11:30:00
【问题描述】:

我正在使用包含大量页面和公式的 Word Docs。

我有一个包含表达式的数组

dim YellowWord(1 to 100) as string

我想从单词文本的开头开始查找这些单词中的每一个,并查看该单词或表达式后跟一个或多个括号中的数字的实例 示例: Yellowword(2)="蓝色桌子"

使用通配符我可以找到:文本中的蓝色表格 (34, 23)。 我想要的是填充另一个数组:

yellowwood_reference(2) = "(34, 23)"

我的代码是这样的:

for i=1 to NRofYellowWords
with active document.content.find
    .clearformating
    .text = yellowWord(i) & " " & "\((*)\)"
    with .replacement
       .clearformating
       .text = yellowWord(i) & "(\1)"
       'HERE IS WHERE I WANT TO SAY TO WORD:
       'PUT THAT PART "(\1)" INTO A VARIABLE YELLOWWORD_REFERENCE(i)
       'HOWW??????
       .font.color = wdcolorred 
       'here i changed the color of the string with the references into red.
    end with
.fordward = true
.wrap = wdfindcontinue
.format = true
.matchcase = false
.matchewholeword = false
.matchwildcards = true
.matchsoundslike = false
.matchallwordforms= false
.execute replace:=wdreplaceall
end with
next i

上面的代码有几个问题: 第一个是我用大写字母写的,把通配符的引用变成一个变量。 第二个是文本中可能有很多 YellowWord(2) 出现,我只需要/想要第一个参考,而不是其余的。这意味着代码在将值 "(24, 26)" 传递到另一个数组后第一次找到 蓝色表 (24,26) 时,代码应该继续而不是在文本中寻找更多蓝色表格的实例。

顺便说一句,我使用了通配符,因为可能存在引用很简单而不是括号的情况,所以我必须使用不同的通配符运行两次。

顺便说一句,你可以想象,一旦我得到数组 yellowWord_reference(i),我就会在没有引用的 YellowWord 实例的地方添加引用。

我非常感谢帮助,因为我确实点击了许多网站但收效甚微。

非常感谢

干杯

PS:如果您认为有更好的方法可以在不使用 .find 的情况下完成所有这些操作,请提及,我是 Ms-Word 中的新手,并且来自 VBA Excel,我很难弄清楚选择点在哪里.

【问题讨论】:

    标签: vba replace ms-word wildcard


    【解决方案1】:

    我修改了你的代码,如果它找到你的“单词”,它将捕获后面的数字。

    由于编译错误的数量,您发布的代码将永远无法工作......强烈建议您开始使用“选项显式”并发布实际代码,而不是自己输入。

    其他说明:

    1. 数字括在括号 () 中 - 不是括号 []
    2. 您使用的是“全部替换”;如果您只想要第一次出现,请从 '...All' 更改
    3. 我删除了“红色字体”和“替换”...如果需要,将其重新添加。
    4. 您的代码将删除单词和数字之间的空格 - 这是您想要的吗?

    代码如下:

    Option Explicit
    
    Sub Find_Words()
    Dim yellowWord(100) As String
    Dim yellowwood_reference(100) As String
    Dim NRofYellowWords     As Integer
    Dim i                   As Integer
    Dim lS          As Long
    Dim lE          As Long
    Dim sFound      As String
    Dim rng         As Range
    
        yellowWord(1) = "blue table"
        yellowWord(2) = "little"
        yellowWord(3) = "big"
        yellowWord(4) = "xxx last xxx"
    
        NRofYellowWords = 4
    
        Set rng = ActiveDocument.Range
    
        For i = 1 To NRofYellowWords
            With rng.Find
                .Text = yellowWord(i) & " " & "\((*)\)"
                With .Replacement
                    .Text = yellowWord(i) & "(\1)"
                End With
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute
                If .Found Then
                    ' Find (numbers) & save
                    lS = InStr(rng.Start, ActiveDocument.Range.Text, "(")
                    If lS > 0 Then
                        lE = InStr(lS, ActiveDocument.Range.Text, ")")
                        sFound = Mid(ActiveDocument.Range.Text, lS, lE - lS + 1)
                        yellowwood_reference(i) = sFound
                        Debug.Print "Found: " & yellowWord(i) & vbTab & sFound
                    Else
                        MsgBox "Bad format; missing '('" & vbTab & Mid(ActiveDocument.Range.Text, lS, 50)
                    End If
                Else
                    Debug.Print "Not Found: " & yellowWord(i)
                End If
            End With
        Next i
    
        Debug.Print "Finished"
    
    End Sub
    

    【讨论】:

    • 嗨,韦恩,感谢您的回答。它完美地工作。我必须继续努力才能继续我的想法。确实,我理解您对编辑的意思。我只是在没有从 VBA 编辑器复制的情况下编写它。我仍然有与您的代码相关的一个问题。在 instr 的帮助下的循环中,您在 activedocument.range.text 中寻找一个左括号“(”。好吧,我不明白它在这里的工作方式,因为我的直觉告诉我 activedocument.range.text 是整个文本文档而不是借助通配符找到的文本。你能说明一下吗?
    • 换句话说,从“我的逻辑” lS = InStr(rng.Start, ActiveDocument.Range.Text, "(") 将始终返回第一个括号的位置,但显然不是。
    • 查看参数 'rng.Start' 它将被设置为找到第一个 YELLOWWORD 匹配的位置,因此代码会在该单词之后查找第一个父项。
    • *父母不是父母
    • rng.start 等价于 activedocument.range.start,因为 Set rng = ActiveDocument.Range。只有当 .find 修改 range 的起始位置时,它才会对我有意义。无论如何,我对 MSWord VBA 还是很陌生。使用 Excel 单元格让生活变得如此简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多