Home > Software design >  Macro to extract data from Word table to Excel
Macro to extract data from Word table to Excel

Time:11-02

Good morning,

I need a Macro in Excel that can extract data from specific tables, columns and rows in my Word doc.

So here is my Excel file and in the 2nd row down I have stipulated which table, row and column data I need returned to that cell from my Word doc (also shown).

Excel file

Word doc page 1

Word doc page 2

I am not a programmer, so I tried Quick Parts and AutoText but didn't get very far.

I would grateful for your help :)

CodePudding user response:

Can you try this?

Sub WordToExcel()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim x As Integer
Dim strFilename As String
Dim strFolder As String
Dim temp As String

Set wdApp = New Word.Application
'initialise counter
x = 1
'search for first file in directory
strFolder = "C:\Test\"
strFilename = Dir(strFolder & "*.doc*")
'amemd folder name
Do While strFilename <> ""
Set wdDoc = wdApp.Documents.Open(strFolder & strFilename)
temp = wdDoc.Tables(1).Cell(2, 1).Range.Text 'read word cell
Range("A2").Offset(x, 0) = temp
temp = wdDoc.Tables(1).Cell(2, 2).Range.Text 'read word cell
Range("A2").Offset(x, 1) = temp
'etc
temp = wdDoc.Tables(1).Cell(2, 3).Range.Text 'read word cell
Range("A2").Offset(x, 2) = temp
temp = wdDoc.Tables(1).Cell(2, 4).Range.Text 'read word cell
Range("A2").Offset(x, 3) = temp

wdDoc.Close
x = x   1
strFilename = Dir
Loop
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub

Setup:

enter image description here

Or...you can try this very primitive solution...

Sub ImportFromWord()

  'Activate Word Object Library

  Dim WordDoc As Word.Document

  Set WordApp = CreateObject("word.application") ' Open Word session

  WordApp.Visible = False 'keep word invisible
  Set WordDoc = WordApp.Documents.Open("C:\Word.doc") ' open Word file

  'copy third row of first Word table
  WordDoc.Tables(1).Rows(3).Range.Copy

  'paste in Excel
  Range("A1").PasteSpecial xlPasteValues

  WordDoc.Close 'close Word doc
  WordApp.Quit ' close Word

End Sub
  • Related