+ Reply to Thread
Results 1 to 2 of 2

Import tables from multiple Word files into a sheet of Excel

  1. #1
    Registered User
    Join Date
    06-26-2012
    Location
    Rome
    MS-Off Ver
    Excel 2010
    Posts
    6

    Question Import tables from multiple Word files into a sheet of Excel

    Hello, a little help with this code, please. This macro copies data from a single Word file table into a worksheet of Excel. It prompts you for the word document as well as the table number if Word contains more than one table (not my code, taken from StackOverflow).

    Problem is: I have hundreds of the same Word file and I need to import them all. The macro should ask only once the number of the table to import (because the type of Word file are all the same) and then import them all in a single sheet.
    For example: the macro ask me to select a file, I choose 10 files in a folder. It asks me the number of table and I answer "2". Then it imports the second table of each Word in the same Excel sheet.

    Is it possible? Thank you very much.
    Magdy

    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
    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
    Last edited by magdy; 06-26-2012 at 12:03 PM.

  2. #2
    Registered User
    Join Date
    06-26-2012
    Location
    Rome
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Import tables from multiple Word files into a sheet of Excel

    Hi again, after spending two days searching for a possible solution I've found only this macro but it gives a mistake at the line in red.
    Is anyone able to understand what is wrong with that or try to merge the two macros so to solve the problem.
    According to the number of posts, the solution would be popular and appreciated.
    Again: the macro should import tables from multiple Word files into a single Excel sheet. The previous macro imports one table from a single Word document while the following macro should import tables from several Word documents but it doesn't work, it shows an error in the red line.
    Thank you very much for your time!

    Sub GetWordDocContents()
    Dim oWord As Object
    Dim vFiles
    Dim iFile As Integer
    Dim R As Range
    vFiles = Application.GetOpenFilename("Word files (*.doc*),*.doc*",Title:="Please select the files you want to copy from", MultiSelect:=True)
    If TypeName(vFiles)="Boolean" Then Exit Sub ' Cancelled
    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    Set R = Worksheets.Add.Range("A1")
    For iFile = LBound(vFIles) To UBound(vFiles)
    oWord.Documents.Open vFiles(iFile)
    oWord.ActiveDocument.Tables(1).Select
    oWord.Selection.Copy
    Activesheet.Paste R
    Set R = Cells(ActiveSheet.UsedRange.Rows.Count+1,1)
    oWord.ActiveDocument.Close False
    Next
    oWord.Quit
    Set oWord = Nothing
    ActiveSheet.Columns.Autofit
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1