问题
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).
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 :
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.
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 ^^, )
回答1:
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.
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.
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.
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".)
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
来源:https://stackoverflow.com/questions/57477990/word-through-excel-how-to-select-a-portion-of-text