iraciv94
iraciv94

Reputation: 840

Copying the content of specific word table to excel

I am trying to create a VBA code that take a specific cell in a word document and outputs it into excel. I see lots of discussion on how to transfer all the tables from word to excel online, but is it possible to do it in a selective way?

For example I have a a word document called "example.docx" as such:

enter image description here

How can I transfer the content of the second cell in the second table into the A1 cell into excel?

Take into account that my aim is to loop through documents with the same structure at some point, thus I believe that indexing using the "Selection.Copy" method may not be the most ideal way for this case.

Thanks so much in advance for the help!

Upvotes: 1

Views: 180

Answers (1)

AntiDrondert
AntiDrondert

Reputation: 1149

Should be something like this:

Sub test()
    Dim wsSheet As Worksheet
    Dim WordApp As Object, WordDoc As Object

    Set wsSheet = ThisWorkbook.Worksheets("Sheet1")
    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = WordApp.Documents.Open("C:\Test\example.docx")
    wsSheet.Range("A1").Value = WordDoc.Range.Tables(2).Range.Cells(2).Range.Text
    WordApp.Quit
End Sub

Upvotes: 3

Related Questions