Modifying a VB Macro to Process a Batch of Word Files

tshipman44

New Member
Joined
Jul 27, 2011
Messages
2
I have a VB macro to extract data from a Word Doc which contains a Table that I built based on one listed here. However, I have several thousand Word Docs (each with just one table), that I need to capture data from. It would be much faster to process a batch of them.

Can one of the VB Experts help with modifying the code below to process all the documents ina folder?

Thanks in advance.

Code:
Sub ImportWordTables()
'Imports cells (3,2) and (4,2) from Word document Tables 1-10
   Dim wdDoc         As Object
   Dim wdFileName    As Variant
   Dim TableNo       As Integer  'number of tables in Word doc
   Dim iTable        As Integer  'table number index
   Dim iRow          As Long     'row index in Excel
   Dim iCol          As Integer  'column index in Excel
Dim ix  As Long
   ix = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
LastRow = ix
   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"
           
           'Else TableNo is actual number of tables between 1 and 9
      End If
             
      For iTable = 1 To TableNo
         With .tables(iTable)
            'copy cell contents from Word table cells to Excel cells in column B and C
            Cells(ix + 1, "A") = WorksheetFunction.Clean(.Cell(1, 2))
            Cells(ix + 1, "B") = WorksheetFunction.Clean(.Cell(2, 2))
            Cells(ix + 1, "C") = WorksheetFunction.Clean(.Cell(3, 2))
            Cells(ix + 1, "D") = WorksheetFunction.Clean(.Cell(4, 2))
            Cells(ix + 1, "E") = WorksheetFunction.Clean(.Cell(5, 2))
            Cells(ix + 1, "F") = WorksheetFunction.Clean(.Cell(6, 2))
            Cells(ix + 1, "G") = WorksheetFunction.Clean(.Cell(6, 3))
            Cells(ix + 1, "H") = WorksheetFunction.Clean(.Cell(7, 2))
            Cells(ix + 1, "I") = WorksheetFunction.Clean(.Cell(8, 2))
            Cells(ix + 1, "J") = WorksheetFunction.Clean(.Cell(9, 2))
            Cells(ix + 1, "K") = WorksheetFunction.Clean(.Cell(10, 2))
Cells(ix + 1, "L") = WorksheetFunction.Clean(.Cell(13, 2))
         End With
         Next iTable
   End With
   
   Set wdDoc = Nothing
   
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Sorry to bump my own message.

It seems like the answer is changing the line here:

Code:
   wdFileName = Application.GetOpenFilename("Word files (*.doc*),*.doc*", , _

to allow for MultiSelect to be True. However, when I do this, it still doesn't allow me to select multiple files.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top