首页 > 解决方案 > Word 通过 Excel - 如何选择文本的一部分?

问题描述

我正在研究一个 Excel-Word VBA 代码,该代码贯穿多个 Word 文件并为 Excel 表(发生 VBA 代码的地方)提取数据。

在每个 Word 文件中都会有一个“工具”描述,然后是该工具的参考(9 个字符加上一个版本字符),如下所示:

在此处输入图像描述

这可以与文本内联,在两个段落中,如“工具 1”,或在表格单元格中,如“工具 2”.... 参考旁边/之后包含图像。

当然,可以有不止一个工具...所以,表格一个接一个,“纯文本”将由图像或段落分隔。

因此,我的目标是提取工具编号和参考代码(始终是不同的),以便在 Excel 表格中,每个文件都有一行,每个工具编号为一列,交叉点处的参考两者:-)

我想选择“工具”和参考末尾之间的所有文本,然后使用它来提取工具编号和参考将很容易。

我已经尝试了几件事,但我不是最好的“查找”功能,正如你将看到的那样^^

oApp.Selection.HomeKey Unit:=wdStory  'Going back to beginning of the word document (last search sent us to the bottom)

            With oApp.Selection.Find 'Searching for the words 'Reference : ' as the ref is just after it
                .Text = "Reference :"
                .Forward = True
                .MatchWholeWord = True
                .Execute  'Lunching the search
            End With

            RefFind = oApp.Selection.EndKey

'A piece of code is surely missing in there


            With oApp.Selection.Find 'Searching for the words 'Tool'
                .Text = "Tool"
                .Forward = False
                .MatchWholeWord = True
                .Execute
            End With
            ToolFind = oApp.Selection.HomeKey
            'ToolFind = oApp.Selection.Find.Execute  'Lunching the search
            'oApp.Selection.Collapse Direction:=wdCollapseEnd

            'oApp.Select.

'No idea what to put there... It obviously will be in a loop that isn't represented here ;-)

如您所见,我首先搜索“参考:”,然后搜索“工具”一词。事实上,单词文件中可以使用“工具”这个词,但是如果我找到“参考:”(不太可能出现),我知道前面的“工具”是好的:- )

所以?我怎么能简单地选择全部?在我的脑海里变得像一个凌乱的迷宫(我正在学习^^,)

标签: excelvbams-wordfind

解决方案


用于Selection这样的事情是非常困难的。最好使用两个Range对象:一个用于参考,另一个用于工具。

下面的代码根据问题中定义的逻辑执行此操作,首先搜索参考,然后返回到工具。对于每个搜索词,使用不同的Range对象。

与 一样SelectionRange对找到的文本的更改。因此,在执行向后搜索之前,Range第二个术语的设置为找到的参考。Duplicate

如果这也找到了,那么 firstRange将扩展到其段落的末尾以及返回到 second 的开头Range。(但是请注意,您可能很想使用单独的范围,将第二个范围扩展到其段落的末尾 - 然后您的工具和参考已经“拆分”了。)

最后,在Range再次运行搜索之前将第一个折叠到其终点,以便它在第一个“查找”之后开始。

Sub FindTermBAckwardsSecondTerm()
    Dim rngFindFirst As Word.Range, rngFindSecond As Word.Range
    Dim firstTerm As String, secondTerm As String
    Dim foundFirst As Boolean, foundSecond As Boolean

    firstTerm = "Reference"
    secondTerm = "Tool"
    Set rngFindFirst = ActiveDocument.content
    Do
        With rngFindFirst.Find
            .Text = firstTerm
            .Forward = True
            .Wrap = wdFindStop
            foundFirst = .Execute
            If foundFirst Then
                Set rngFindSecond = rngFindFirst.Duplicate
                rngFindSecond.Collapse wdCollapseStart
                With rngFindSecond.Find
                    .Text = secondTerm
                    .Forward = False
                    .Wrap = wdFindStop
                    foundSecond = .Execute
                    If foundSecond Then
                        rngFindFirst.MoveEnd wdParagraph, 1
                        rngFindFirst.Start = rngFindSecond.Start
                        Debug.Print rngFindFirst
                    End If
                End With
            End If
            rngFindFirst.Collapse wdCollapseEnd
        End With
     Loop While foundFirst
End Sub

推荐阅读