stevehoops5
New Member
- Joined
- Nov 19, 2013
- Messages
- 2
Hi All,
I'm having trouble looping through all of the word files in a folder. I am trying to pull a table from each word file in a folder. My ImportWordTable sub works fine when i have the wdFileName picked with a browser. It doesn't seem to do anything when I try to set it to MyFile so that I can loop through. The problem seems to be with "wdFileName = MyFile" which is in the ImportWordTable sub. Any Ideas on what I'm doing wrong?
:
I'm having trouble looping through all of the word files in a folder. I am trying to pull a table from each word file in a folder. My ImportWordTable sub works fine when i have the wdFileName picked with a browser. It doesn't seem to do anything when I try to set it to MyFile so that I can loop through. The problem seems to be with "wdFileName = MyFile" which is in the ImportWordTable sub. Any Ideas on what I'm doing wrong?
Code:
Sub DirLoop()
Dim MyFile As Variant, Sep As Variant
Call Choose
Sep = Application.PathSeparator
MyFile = Dir(refreshLocation & Sep & "*doc")
Do While MyFile <> ""
Call ImportWordTable
MyFile = Dir()
Loop
End Sub
Sub Choose()
Dim refreshLocation As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
refreshLocation = .SelectedItems(1)
End With
End Sub
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
Dim lasty As Integer
lasty = LastRow(ActiveSheet)
wdFileName = MyFile
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
With .tables(.tables.Count)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
If iRow = 1 Then GoTo Nexts Else
Cells(iRow + lasty, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Nexts:
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub