gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:37:15 26 4
gpt4 key购买 nike

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

到目前为止,我让 Excel 宏一次打开每个 Word 文档,并找到与我需要的内容相邻的文本。

我现在需要:

  • 移动到 Word 表格中的相邻单元格。我在想wdApp.Selection.MoveLeft Unit:=wdCell (或 MoveRight )其中 wdApp 为 Word.Application
  • 复制单元格的内容。我在想wdApp.Selection.CopywdDoc.Word.Range 之类的东西在哪里 wdDocWord.Document但我无法选择整个单元格的内容。
  • 将其粘贴到 Excel 中的变量中。在这里,我不知道如何将剪贴板复制到 Excel 变量中。
  • 最佳答案

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

    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 范围。

    关于excel - 如何使用 Excel 宏将一段文本从 Word 复制到 Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7338385/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com