【发布时间】: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 ;-)
如您所见,我首先搜索“参考:”,然后搜索“工具”一词。 事实上,单词文件中可以使用“工具”一词,但如果我找到“参考:”(不太可能出现),我知道前面的“工具”是好的:- )
所以?我怎么能简单地选择全部?在我的脑海里变得像一个凌乱的迷宫(我正在学习^^,)
【问题讨论】:
-
参考代码是否遵循某种模式?