如何使用Excelmacros将Word中的一段文本复制到Excel?
我需要使用Excelmacros将特定项目的文本(一个或几个单词)从Word(2007)复制到Excel(2007),以获得多个文档。
到目前为止,我有Excelmacros,一次打开每个Word文档,并find我所需要的文字。
我现在需要:
- 移动到Word表格中的相邻单元格。 我在想
wdApp.Selection.MoveLeft Unit:=wdCell
(或MoveRight
)其中wdApp是Word.Application
- 复制单元格的内容。 我想
wdApp.Selection.Copy
和wdDoc.Word.Range
其中wdDoc
是Word.Document
但我不能select整个单元格的内容。 - 将其粘贴到Excel中的variables。 在这里,我不知道如何将剪贴板复制到一个Excelvariables。
已更新显示search文本,然后select相对于其位置的内容:
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
然后将variablesTheContent分配给您所需的Excel范围。