如何使用 Excel 宏将一段文本从 Word 复制到 Excel?

发布于 2024-12-03 13:32:37 字数 532 浏览 0 评论 0原文

我需要使用 Excel 宏将多个文档的特定文本项(一个或几个单词)从 Word (2007) 复制到 Excel (2007)。

到目前为止,我已经使用 Excel 宏一次打开每个 Word 文档,并将文本定位到与我需要的内容相邻的位置。

我现在需要:

  1. 移动到 Word 表格中的相邻单元格。我正在考虑 wdApp.Selection.MoveLeft Unit:=wdCell (或 MoveRight),其中 wdApp 是 Word.Application
  2. 复制单元格的内容。我正在考虑 wdApp.Selection.Copy 和类似 wdDoc.Word.Range 的内容,其中 wdDocWord.Document > 但我无法选择整个单元格内容。
  3. 将其粘贴到 Excel 中的变量中。这里我不知道如何将剪贴板复制到Excel变量。

I need to copy a specific item of text (one or a few words) from Word (2007) to Excel (2007) using an Excel macro, for multiple documents.

So far I have the Excel macro opening each Word document one at a time and locating the text adjacent to what I need.

I now need to:

  1. Move to an adjacent cell in a Word table. I'm thinking wdApp.Selection.MoveLeft Unit:=wdCell (or MoveRight) where wdApp is Word.Application
  2. Copy the contents of the cell. I'm thinking wdApp.Selection.Copy and something like wdDoc.Word.Range where wdDoc is Word.Document but I can't select the whole cells contents.
  3. Paste it into a variable in Excel. Here I don't know how to copy the clipboard to an Excel variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-12-10 13:32:37

更新为显示搜索文本,然后选择相对于其位置的内容:

Sub FindAndCopyNext()

    Dim TextToFind As String, TheContent As String
    Dim rng As Word.Range

    TextToFind = "wibble" 'the text you're looking for to
                          ' locate the other content

    Set rng = wdApp.ActiveDocument.Content
    rng.Find.Execute FindText:=TextToFind, Forward:=True

    If rng.Find.Found Then
        If rng.Information(wdWithInTable) Then
          TheContent = rng.Cells(1).Next.Range.Text      'move right on row
          'TheContent = rng.Cells(1).Previous.Range.Text 'move left on row
          MsgBox "Found content '" & TheContent & "'"
        End If
    Else
        MsgBox "Text '" & TextToFind & "' was not found!"
    End If

End Sub

然后将变量 TheContent 分配给所需的 Excel 范围。

Updated to show searching for text and then selecting content relative to its location:

Sub FindAndCopyNext()

    Dim TextToFind As String, TheContent As String
    Dim rng As Word.Range

    TextToFind = "wibble" 'the text you're looking for to
                          ' locate the other content

    Set rng = wdApp.ActiveDocument.Content
    rng.Find.Execute FindText:=TextToFind, Forward:=True

    If rng.Find.Found Then
        If rng.Information(wdWithInTable) Then
          TheContent = rng.Cells(1).Next.Range.Text      'move right on row
          'TheContent = rng.Cells(1).Previous.Range.Text 'move left on row
          MsgBox "Found content '" & TheContent & "'"
        End If
    Else
        MsgBox "Text '" & TextToFind & "' was not found!"
    End If

End Sub

Then assign the variable TheContent to your required Excel range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文