Microsoft Excel - Importing world table to excel macro

Asked By farrukh on 13-Jan-11 02:03 AM

HI ALL

Please help me i have a code Importing world table to excel macro my problem is i have 4 tables in word the macro runs but it gets the last table  in the excel sheet not all 4 tables any one help please

very thankful


Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
TableNo = wdDoc.tables.Count
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With

Set wdDoc = Nothing

End Sub


my email is furrukh_cancer@yahoo.com please send me answer there......

Jackpot . replied to farrukh on 13-Jan-11 02:38 AM
Hi Farrukh

Try this macro and feedback..


Sub ImportWordTable()
Dim wdDoc As Object, wdFileName As Variant, tblTemp As Object
  
wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")
  
If wdFileName = False Then Exit Sub
  
Set wdDoc = GetObject(wdFileName)
  
For Each tblTemp In wdDoc.tables
tblTemp.Range.Copy
Cells(Rows.Count, "A").End(xlUp).Offset(2).PasteSpecial xlPasteValues
Next
  
Set wdDoc = Nothing
  
End Sub
farrukh replied to Jackpot . on 13-Jan-11 03:20 AM
Dear Jackpot once again thanks for helping me bundle of thanks alotttttttt


Really it works and i m getting all the 4 tables in excel sheet :)

when ever i put my question in www.eggheadcafe.com get the quickest response plus authentic answer...


egg head  user
Farrukh
farrukh replied to Jackpot . on 13-Jan-11 03:33 AM
Dear Jackptot

Please a little amendent more i want to give the specific name of file like abc.doc in the macro that every time it won't ask me to "Browse for file containing table to be imported"

fixed for one abc.doc


Sub ImportWordTable()
Dim wdDoc As Object, wdFileName As Variant, tblTemp As Object
  
wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")
  
If wdFileName = False Then Exit Sub
  
Set wdDoc = GetObject(wdFileName)
  
For Each tblTemp In wdDoc.tables
tblTemp.Range.Copy
Cells(Rows.Count, "A").End(xlUp).Offset(2).PasteSpecial xlPasteValues
Next
  
Set wdDoc = Nothing
  
End Sub


Thanks

Farrukh
Jackpot . replied to farrukh on 13-Jan-11 03:39 AM
Hi Farrukh

Thanks for the feedback..You are most welcome here.

Try the below..Mention the full path and file name as below


Sub ImportWordTable()
Dim wdDoc As Object, tblTemp As Object
 
Set wdDoc = GetObject("c:\abc.doc") 
 
For Each tblTemp In wdDoc.tables
tblTemp.Range.Copy
Cells(Rows.Count, "A").End(xlUp).Offset(2).PasteSpecial xlPasteValues
Next
  
Set wdDoc = Nothing
  
End Sub

farrukh replied to Jackpot . on 13-Jan-11 03:50 AM
Dear Jackpot

Thanks alot the given macro really great and many thanks for your support and quickest help....

May God gives you all the happiness of life

Thanks eggheadcafe
Farrukh
vineet rathore replied to farrukh on 06-Mar-12 12:00 AM
Hi everyone,

I have to import selected tables from a word document which contains no of tables. how can i do it ? i have mentioned separate tables identifiers to make them distinct. Please help me.

Thank you.
farrukh replied to vineet rathore on 06-Mar-12 08:03 AM

Try this

Sub GetWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With

Set wdDoc = Nothing

End Sub


Thanks
farrukh