如何使用 Excel 宏将一段文本从 Word 复制到 Excel? [英] How can I copy one section of text from Word to Excel using an Excel macro?

查看:61
本文介绍了如何使用 Excel 宏将一段文本从 Word 复制到 Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

到目前为止,我已经使用 Excel 宏一次打开每个 Word 文档并找到与我需要的文本相邻的文本.

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

我现在需要:

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

推荐答案

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

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

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

Then assign the variable TheContent to your required Excel range.

这篇关于如何使用 Excel 宏将一段文本从 Word 复制到 Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆