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

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

问题描述

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



我现在需要:

p>


  1. 移动到Word表中的相邻单元格。我在想 wdApp.Selection.MoveLeft单位:= wdCell (或 MoveRight )其中wdApp是 Word.Application

  2. 复制单元格的内容。我在想 wdApp.Selection.Copy 和类似 wdDoc.Word.Range 其中 wdDoc Word.Document ,但我无法选择整个单元格内容。

  3. 将其粘贴到Excel中的变量。这里我不知道如何将剪贴板复制到Excel变量。


解决方案

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

  Sub FindAndCopyNext()

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

TextToFind =wibble'要查找的文本
'找到其他内容

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

如果rng.Find.Found然后
如果rng.Information(wdWithInTable)然后
TheContent = rng.Cells(1).Next.Range.Text'在右边移动
'TheContent = rng.Cells(1).Previous.Range .Text'向左移动
MsgBoxFound content&内容与'
End If
Else
MsgBoxText& TextToFind& ' 没找到!
End If

End Sub

然后分配变量TheContent到您所需的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.

解决方案

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.

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

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