title: Open all .doc files in a folder and apply the code
body: <p>This is the last step of my work. I have designed a code to retrieve tables from a Word document.
  I have written a code which works for each individual Word document. I have thousands of Word documents and I would love to automate the process.</p>
  
  <p>In other words, I would like to run the code for all the .doc files in the same folder but I can't seem to get my head around it.</p>
  
  <p>The (beginning) code looks like this:</p>
  
  <pre><code>Sub importTableDataWord()
  Dim ws As Worksheet
  Set ws = Sheets("Sheet1")
  'We declare object variables for Word Application and document
  Dim wdApp As Object, wddoc As Object
  'Declare a string variable to access our Word document
  Dim strDocName As String
  
  'Designate Word
  'Error handling
  On Error Resume Next
  'Create a Word application if Word is not already open
  Set wdApp = CreateObject("Word.Application")
  wdApp.Visible = True
  strDocName = "C:\Users\PDX\Documents\CBA01.doc"
  'Check relevant directory for relevant document
  'If not found then inform the user and close program
  If Dir(strDocName) = "" Then
      MsgBox "The file " &amp; strDocName &amp; vbCrLf &amp; _
      "was not found in the folder path" &amp; vbCrLf &amp; _
      "C:\our-inventory\.", _
      vbExclamation, _
      "Sorry, that document name does not exist."
  Exit Sub
  End If
  
  'Open/activate the designated Word
  wdApp.Activate
  Set wddoc = wdApp.Documents(strDocName)
  If wddoc Is Nothing Then Set wddoc = wdApp.Documents.Open(strDocName)
  wddoc.Activate
  
  'Define variables to access the tables in the word document
  Dim Tble As Integer
  Dim rowWd As Long
  Dim colWd As Integer
  Dim x As Long, y As Long
  x = 1
  y = 1
  
  
  ''''' FIRST LINE, FIRST TABLE
  
  'Count nb of tables in my Word
  With wddoc
  Tble = wddoc.Tables.Count
      If Tble = 0 Then
      MsgBox "No Tables found in the Word document", vbExclamation, "No Tables to Import"
  Exit Sub
  End If
  
  'Start the looping process to access tables and their rows, columns
  For i = 1 To 1
      With .Tables(i)
      For colWd = 1 To .Columns.Count
      Cells(x, y) = WorksheetFunction.Clean(.cell(1, colWd).Range.Text)
      'Access next column until the end
      y = y + 1
      Next colWd
      'go to next row and start from column 1
      y = 1
      x = x + 1
  End With
  Next
  End With
  
  wdApp.Quit
  End Sub
  </code></pre>
  
  <p>I was thinking to use something similar as below but can't find a way to create a loop to apply the code! </p>
  
  <pre><code>'set folder where all the .doc are located
  myFolder = "C:\Users\PDX\Documents\"
  strFile = Dir(myFolder &amp; "\*.doc", vbNormal)
  While strFrile &lt;&gt; ""
  Set wddoc = wdApp.Documents.Open(Filename:=myFolder &amp; "\" &amp; strFile, AddToRecentFiles:=False, Visible:=True)
  
  'Open/activate the designated Word
  wdApp.Activate
  Set wddoc = wdApp.Documents(strDocName)
  If wddoc Is Nothing Then Set wddoc = wdApp.Documents.Open(strDocName)
  wddoc.Activate
  </code></pre>
  
  <p>But I also thought of creating a sub calling my main sub:</p>
  
  <pre><code>Sub testhello()
  Dim file
  Dim path As String
  
  
  path = "C:\Users\PDX\Documents\"
  
  file = Dir(path &amp; "*.doc")
  Do While file &lt;&gt; ""
  Documents.Open Filename:=path &amp; file
  
  Call importTableDataWord
  
  ActiveDocument.Save
  ActiveDocument.Close
  
  file = Dir()
  Loop
  
  End Sub
  Ideally, 1 w
  </code></pre>
  
  <p><strong>Ideally,</strong> the loop would be such that 1 word table retrieved = 1 spreadsheet, OR, the next table retrieved would appear after the last row.</p>
  
  <p>Any feedback appreciated. If you know about some of it, or have any hint, feel free to propose! </p>