【问题标题】:Get specific words from Word to Excel将特定单词从 Word 获取到 Excel
【发布时间】:2020-05-12 07:14:04
【问题描述】:

我有包含某些序列号的 Word 文档(它们都以 3BMS* 或 7SGA* 开头),我需要将其提取到 Excel 中。每个 Word 文档都包含数百个序列号。

我正在尝试的方法是 VBA 打开 Word 文档并使用搜索来查找包含序列号前四位数字的单词。然后,我将为这两个代码中的每一个运行代码。

我有 VBA 来查找包含搜索词的段落。

While oRange.Find.Execute = True

    oRange.Select
            
    myPara = oDoc.Range(0, oWord.Selection.Paragraphs(1).Range.End).Paragraphs.Count

    CurrRowShtExtract = CurrRowShtExtract + 1

    shtExtract.Cells(CurrRowShtExtract, 1) = oDoc.Paragraphs(myPara).Range

    oRange.Collapse wdCollapseEnd

Wend

如何只返回包含搜索结果的单词,而不是整个段落?

【问题讨论】:

  • 也许使用Regexp?例如this

标签: excel vba


【解决方案1】:

考虑到您之前的代码,类似以下的内容应该可以工作。这将在文档中搜索 3BMS 的每个实例以及紧随其后的字母数字值,并将该值插入到引用的单元格中。然后它将对 7SGA* 执行相同的操作。

' Search the document for the first serial number type
Set oRange = oDoc.Content
Do While oRange.Find.Execute("3BMS[a-zA-Z0-9]{1,}", MatchWildcards:=True)
    CurrRowShtExtract = CurrRowShtExtract + 1
    shtExtract.Cells(CurrRowShtExtract, 1) = oRange.Text
Loop

' Search the document for the second serial number type
Set oRange = oDoc.Content
Do While oRange.Find.Execute("7SGA[a-zA-Z0-9]{1,}", MatchWildcards:=True)
    CurrRowShtExtract = CurrRowShtExtract + 1
    shtExtract.Cells(CurrRowShtExtract, 1) = oRange.Text
Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多