Results 1 to 2 of 2

Import tables from multiple Word files into a sheet of Excel

Threaded View

magdy Import tables from multiple... 06-26-2012, 11:11 AM
magdy Re: Import tables from... 06-27-2012, 03:53 AM
  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.

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