通过Excel的Word-如何选择部分文本? [英] Word through Excel - How to select a portion of text?

查看:119
本文介绍了通过Excel的Word-如何选择部分文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on an Excel-Word VBA code that runs through several Word files and extracts data for the Excel table (where the VBA code takes place).

在每个Word文件中,都会有一个工具"说明,后跟对此工具的引用(9个字符加一个版本字符),如下所示:

In each of the Word files there will be a "tool" description followed by a reference for this tool (9 char plus a version char) like this :

这可以与文本内联,在类似于工具1"的两段中,也可以在诸如工具2"的表格单元格中....在引用的旁边/之后包含图像.

This can be inline with the text, in two paragraphs like for the "tool 1", or in a table cell like the "Tool 2".... An image is included next/after the reference.

当然,可以有多个工具...因此,表格彼此相邻,并且纯文本"将由图像或段落分隔.

Of course, there can be more than one Tool... So, the tables follow one another, and the "plain text" will be separated by images or paragraphs.

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

Therefore, my goal is to extract the tool number and the reference code (which is always a different one), so that in the Excel table there will be a row for each file, a column for each tool number and the reference at the intersection of both :-)

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

I want to select all the text between "Tool" and the end of the reference, then it will be easy to work with it to extract the Tool number and reference.

我已经尝试了好几种方法,但是使用查找"功能并不是最好的方法,因为您会看到^^

I've already tried several things but I'm not the best one with the "find" function as you will see ^^

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 ;-)

如您所见,我首先搜索了参考:",然后又搜索了工具"一词. 实际上,单词工具中可以使用工具"一词,但是如果我找到了引用:"(不太可能出现),我知道前面的工具"是一个很好的工具:- )

As you see, I searched for "Reference : " first, then searched back the word "Tool". In fact, the word "tool" can be used in the word files, but if I found the "reference : " (which is way less likely to appear), I know that the "Tool" just before is the good one :-)

是吗?我怎么能简单地选择全部呢?我的脑子里就像是一个混乱的迷宫(我正在学习^^,)

So? How could I simply select that all? It's getting like a messy labyrinth in my head (I'm learning ^^, )

推荐答案

使用Selection进行此类操作非常困难.最好使用两个Range对象:一个用于引用,另一个用于工具.

Using Selection for something like this is very difficult. Better to work with two Range objects: one for the Reference, the other for the Tool.

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

The code below does that, basing on the logic defined in the question, first searching Reference, then working backwards to Tool. For each search term a different Range object is used.

Selection一样,Range更改为找到的文本.因此,在执行向后搜索之前,第二项的Range设置为找到的参考的Duplicate.

As with Selection, the Range changes to the found text. So the Range for the second term is set to the Duplicate of the found Reference before the search backwards is executed.

如果也找到了此内容,则第一个Range会扩展到其段落的末尾,然后又扩展到第二个Range的开头. (但是,请注意,您可能希望使用单独的范围,将第二个范围扩展到其段落的末尾-然后您已经将工具和参考"拆分"了.)

If this also found, then the first Range is extended to the end of its paragraph as well as back to the start of the second Range. (Note, however, that you may well want to work with the separate ranges, extending the second range to the end of its paragraph - then you have Tool and Reference already "split".)

最后,在再次运行搜索之前,第一个Range会折叠到其端点,以便在 第一个查找"之后开始.

Lastly, the first Range is collapsed to its end-point before the search is run again so that it begins after the first "find".

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

这篇关于通过Excel的Word-如何选择部分文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆