【问题标题】:excel macro search for word and copy sentenceexcel宏搜索单词并复制句子
【发布时间】:2014-04-24 10:10:03
【问题描述】:

希望有人能帮我解决这个问题。

我有两个文档,一个是 Word,一个是 Excel。在 word 文件中,我有一个项目列表,例如:

标题副标题

1. Name

   Address:

   Phone number:

2. Name

   Address:

   Phone number:

3. Name

   Address:

   Phone number:

在excel文件中,我在D列中有一个单词列表。我想做的是从D列中取出单词,在Word文档中搜索它,然后将“地址:”后面的句子复制到“.”,将其放在C列(即左侧一个单元格),然后将“电话号码:”后面的句子复制到“。”并将其放在 B 列中。

我无法真正理解的部分是从第一组姓名、地址和电话号码到下一组。

有人可以帮助我了解如何执行此操作的宏吗?

我曾想过从这里扩展它:

Sub wordSearch()

' Purpose: display the text between (but not including)
' the words "Title" and "Address" if they both appear.
    Dim rng1 As Range
    Dim rng2 As Range
    Dim strTheText As String

    Set rng1 = ActiveDocument.Range
    If rng1.Find.Execute(FindText:="Example:") Then
        Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
        If rng2.Find.Execute(FindText:=".") Then
            strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
            MsgBox strTheText
        End If
    End If

End Sub

【问题讨论】:

  • 请说明,需要从Excel D列单元格中获取文本,然后在Word中找到名称与该文本完全相同的项目,并将电话和地址复制到Excel中?
  • 你好 omegastripes,没错。

标签: macos vba excel


【解决方案1】:

例如,下面的 Excel 子程序从与 Excel 文件位于同一文件夹中的 catalog.doc 获取整个文本,使用 RegExp 解析文本,循环访问联系人并将其放入 Dictionary,然后循环遍历D2:D10 单元格并将匹配名称的适当数据分别分配给 CB 列。在 MS Office 2003、Windows 7 HB 中测试。

Option Explicit

Sub GetFromWord()

    ' Tools - References - add these:
    ' Microsoft Word 11.0 Object Library
    ' Microsoft VBScript Regular Expressions 5.5
    ' Microsoft Scripting Runtime

    Dim strCont As String
    Dim objCatalog As Scripting.Dictionary
    Dim objMatch As IMatch2
    Dim objElt As Range

    With New Word.Application
        .Documents.Open ThisWorkbook.Path & "\catalog.doc"
        With .ActiveDocument.Range
            .WholeStory
            strCont = .Text
        End With
        .Quit
    End With
    Set objCatalog = New Scripting.Dictionary
    With New RegExp
        .Pattern = "\d+\.[ \t]*([^\n\r]*)\s*Address:[ \t]*([^\n\r]*)\s*Phone number:[ \t]*([^\n\r]*)\s*"
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        For Each objMatch In .Execute(strCont)
            objCatalog.Add objMatch.SubMatches(0), Array(objMatch.SubMatches(1), objMatch.SubMatches(2))
        Next
    End With
    For Each objElt In Range("D2:D10")
        With objElt
            If objCatalog.Exists(.Cells(1, 1).Value) Then
                .Offset(0, -1) = objCatalog(.Cells(1, 1).Value)(0)
                .Offset(0, -2) = objCatalog(.Cells(1, 1).Value)(1)
            End If
        End With
    Next
End Sub

注意,Word 中重复的联系人会导致错误,不实施额外检查。

UPD:如果早期绑定有任何问题,您可以使用后期绑定CreateObject(ProgID),如下所示,但这不是 VBA 中的最佳做法:

Option Explicit

Sub GetFromWordLBind()

    Dim strCont As String
    Dim objCatalog, objMatch, objElt As Object

    With CreateObject("Word.Application")
        .Documents.Open ThisWorkbook.Path & "\catalog.docx"
        With .ActiveDocument.Range
            .WholeStory
            strCont = .Text
        End With
        .Quit
    End With
    Set objCatalog = CreateObject("Scripting.Dictionary")
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d+\.[ \t]*([^\n\r]*)\s*Address:[ \t]*([^\n\r]*)\s*Phone number:[ \t]*([^\n\r]*)\s*"
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        For Each objMatch In .Execute(strCont)
            objCatalog.Add objMatch.SubMatches(0), Array(objMatch.SubMatches(1), objMatch.SubMatches(2))
        Next
    End With
    For Each objElt In Range("D2:D10")
        With objElt
            If objCatalog.Exists(.Cells(1, 1).Value) Then
                .Offset(0, -1) = objCatalog(.Cells(1, 1).Value)(0)
                .Offset(0, -2) = objCatalog(.Cells(1, 1).Value)(1)
            End If
        End With
    Next
End Sub

【讨论】:

  • 您好 Omegastripes,我尝试将您的修复与 word 97-2004 (.doc) 一起使用,但出现以下编译错误:未定义用户定义类型。它指向以下代码行: Dim objCatalog As Scripting.Dictionary 如果我做错了什么,您能告诉我吗?此外,word 文档采用 2011 格式的 word (.docx)。我需要改变什么吗?在此先感谢并感谢您对此进行尝试。
  • 由于使用了早期绑定 - 您必须在项目中添加一些组件:在 Excel VBA 编辑器中打开您的项目,按 Menu - Tools - References,然后检查组件 I'在我的代码开头列出。选择合适的 Word Object Lib 版本(11.0 对应 2003)。对于*.docx,无需额外更改,只需指定实际文件名而不是catalog.doc
  • 您好 omegastripes,我收到以下错误:运行时错误 429:无法创建 ActiveX 组件。使用 CreateObject("Word.Application") 由调试器突出显示。我做错了吗?
  • 这出乎意料的失败.. 检查您的 MS Office 是否安装正确。 Word.Application 键必须存在于 HKEY_CLASSES_ROOT 注册表分支中,才能使用该 ProgID 创建 ActiveX 实例。您是否尝试添加对项目的引用以执行早期绑定(我的回答中的第一个子项)? Word|Excel - Menu - Help - About 的 MS Office 版本是什么?
  • 您好 omegastripes,我查看了引用选项,唯一可选择的选项是: Visual basic for applications Microsoft Excel 14.0 Object library Microsoft forms 2.0 Object library Microsoft office 14.0 Object library Microsoft visual basic of applications Microsoft word 14.0 对象库 图14 类型库 Microsoft graph 14.0 对象库 Microsoft powerpoint 14.0 对象库 OLE 自动化 我使用的 excel 版本是:Microsoft excel for Mac 2011 Version 14.3.9 这是一个带密钥的正版副本。再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
相关资源
最近更新 更多