How to copy a single section of text from Word to Excel using Excel macro?

I need to copy specific text (one or several words) from Word (2007) to Excel (2007) using an Excel macro for multiple documents.

So far, I have an Excel macro that opens each Word document one at a time and arranges the text adjacent to what I need.

Now I need:

  • Move to a neighboring cell in a Word table. I think wdApp.Selection.MoveLeft Unit:=wdCell (or MoveRight ), where wdApp Word.Application
  • Copy cell contents. I think wdApp.Selection.Copy and something like wdDoc.Word.Range where wdDoc is a Word.Document , but I cannot select the contents of the whole cell.
  • Paste it into a variable in Excel. Here I do not know how to copy the clipboard to an Excel variable.
+4
source share
1 answer

Updated to show text search and then select 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 TheContent variable to the required Excel range.

+4
source

All Articles