Sunday, November 24, 2013

How to Extract Tables From Multiple Word Documents Import Them to Excel


1. Use Windows Explorer to copy a Word file containing at least one table to the folder 'C:\.' Rename the file as 'table.docx.' You'll write an introductory program that will read a table cell from this file.
2. Click the 'Developer' tab, then click the 'Visual Basic' button to enter the Visual Basic programming environment. Paste the following program into VB code window. This program creates a link to Word, which allows excel to use Word's virtual objects just as though the program were running inside a Word document. This process is called 'Automation.'Public Sub LoadWordTablebak()Dim pgmWord As Word.ApplicationSet pgmWord = CreateObject('Word.Application')pgmWord.Documents.Open ('c:\table.docx')MsgBox pgmWord.ActiveDocument.Tables(1).Cell(1, 1)pgmWord.ActiveDocument.ClosepgmWord.QuitEnd Sub
3. Click the 'Tools' menu, then click the 'References' command. Scroll through the window that appears to locate the 'Microsoft Word' item, then click the checkbox for that item. This action lets Excel access the visual objects in a Word document.
4. Click any statement in the program, then click the 'Run' menu's 'Run' command. The program will display a message box showing the contents of a table cell in a Word document. You'll now expand the program to load in a complete table from any Word document.
5. Paste the following revised program below the 'End Sub' statement of the original program. This program prompts the user for the filename of a Word document. The user must also type in the number of a table within the document. The program uses the automation method of the first program to load copy the specified table cell by cell into an Excel spreadsheet.Public Sub LoadWordTable2()Dim docname As StringDim TableId As IntegerDim c, r, startRow As IntegerDim curCellDim pgmWord As Word.ApplicationSet curCell = ActiveCellSet pgmWord = CreateObject('Word.Application')docname = InputBox('Enter Word document name')docname = InputBox('Enter Word document name')While (docname
'')TableId = InputBox('Enter table number')pgmWord.Documents.Open ('c:\table.docx')With pgmWord.ActiveDocument.Tables(TableId)startRow = ActiveCell.RowFor c = 1 To .Columns.CountFor r = 1 To .Rows.CountcurCell.Value = .Cell(r, c)'Move to next rowSet curCell = curCell.Offset(1, 0)Next r'Move to next columnSet curCell = Cells(startRow, curCell.Column 1)Next cEnd WithpgmWord.ActiveDocument.Closedocname = InputBox('Enter Word document name')WendpgmWord.QuitEnd Sub
6. Click the 'Excel' application icon in the Windows taskbar to return to Excel, then click the 'Macros' button of the 'Developer' tab. Double-click the 'LoadWordTable' macro to run that macro.
7. Type the name of a Word document containing a table when the prompt to do so appears. Type the number of the table you want when the prompt for that table appears. For example, if you want the second table in the document, type '2.' Excel will load the table into the current spreadsheet.
8. Continue loading Word tables with the program as needed. Press 'Enter' at the 'document name' prompt to terminate the program.

Blogger news