title: Excel VBA to Open Multiple Word files in a loop
body: <p>I apologize in advance for the newbie question -- most of my VBA experience is in Excel, or Word to Excel. In this case, I am going from Excel to Word. I am trying to capture some data off of some Word forms and store it in an Excel file. </p>
  
  <p>Right now, my code works for the first document in the folder, but after that, it hoses up with an automation error "the server threw an exception" (goo!)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Dim objWordApp As Object
  
  strCurFileName = Dir(strFilePath)
  
  Set objWordApp = CreateObject("word.application")
  objWordApp.Visible = True
  
  Do While strCurFileName &lt;&gt; ""
  
      objWordApp.documents.Open strFilePath &amp; strCurFileName
      objWordApp.activedocument.Unprotect password:="testcode"
  
      {EXCEL PROCESSING HERE}
  
      strCurFileName = Dir
      objWordApp.activedocument.Close 0
  
  Loop
  
  objWordApp.Quit
  Set objWordApp = Nothing
  </code></pre>
  
  <p>I notice that the code works fine if I quit the app and set the object = nothing within the loop. But the way it is now, it bombs-out on the second file in the folder on the "objWordApp.documents.Open strFilePath &amp; strCurFileName" line. </p>
  
  <p>Can I open and close Word documents in a loop without having to create the object over and over? It's really slow when I do it that way.</p>
  
  <hr>
  
  <p>Thanks for the help -- I like your way much better. Unfortunately, I get the same result. The program dies the second time through the loop on the line that reads:</p>
  
  <pre><code>Set objWordDoc = objWordApp.Documents.Open(objFile.Path)
  </code></pre>
  
  <p>The error that I get is:</p>
  
  <p><strong>Run-time Error -2147417851 (80010105)
  Automation Error
  The server threw an exception.</strong></p>
  
  <p>I tried your code on regular word docs (not the ones I'm processing) and it worked fine. The docs I'm running have form fields and macros --  not sure if that makes a difference. I have set the macro security in Word to both "low" and "very high" to make sure the other macros don't interfere. </p>
  
  <p>I just can't figure it out why it works for the first doc and then not the next. I even cloned the first doc but it made no difference.</p>
  
  <hr>
  
  <p>Still no luck, though. The only thing I can get to work is if I completely wipe the objects and re-create them every time I want to open a file.</p>
  
  <pre><code>Set objFolder = FSO.GetFolder(strFilePath)
  
  For Each objFile In objFolder.Files
  
      Set objWordApp = CreateObject("word.application")
      objWordApp.Visible = True
  
      If Right(objFile.Name, 4) = ".doc" Then
          Set objWordDoc = objWordApp.documents.Open(Filename:=objFile.Path, ConfirmConversions:=False, _
              ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
              PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
              WritePasswordTemplate:="", Format:=wdOpenFormatAuto)
  
          [Process DOC]
  
          objWordDoc.Close 0, 1
      End If
  
      Set objWordDoc = Nothing
      objWordApp.Quit
      Set objWordApp = Nothing
  
  Next
  </code></pre>
  
  <p>I'm not sure why that works and why it won't work the other way. If I have to go this route, I can -- it just seems really slow and inefficient. Is this a bad idea?</p>