【问题标题】:Word through Excel - How to select a portion of text?Word 通过 Excel - 如何选择文本的一部分?
【发布时间】:2019-08-13 12:44:29
【问题描述】:

我正在编写一个 Excel-Word VBA 代码,该代码运行多个 Word 文件并为 Excel 表(VBA 代码发生的地方)提取数据。

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

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

当然,可以有多个Tool...所以,表格一个接一个,“纯文本”将被图像或段落分隔。

因此,我的目标是提取工具编号和参考代码(始终是不同的),以便在 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 ;-)

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

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

【问题讨论】:

  • 参考代码是否遵循某种模式?

标签: excel vba ms-word find


【解决方案1】:

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

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

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

如果也找到了,则第一个 Range 将扩展到其段落的末尾,并返回到第二个 Range 的开头。 (但是请注意,您可能很想使用单独的范围,将第二个范围扩展到其段落的末尾 - 然后您的 Tool 和 Reference 已经“拆分”了。)

最后,第一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    相关资源
    最近更新 更多