title: VBA: Microsoft Word process does not exit after combining many Word files into one
body: <p>I'm trying to merge many Word files into one. I am doing this inside a VBA routine in MS Excel. The Word files are all in a folder named "files" and I want to create a new file "combinedfile.docx" in a folder one-level above that. The problem I'm facing is regarding how the Word process behaves after merging the files (whether or not it exits after the execution of the VBA function). On some machines, this process works fine (except that it has page 2 and the last page as blank), while on some others, the merged document contains a blank page and the Process Manager shows the Word process started by the VBA function as still running.</p>
  
  <ol>
  <li><p>I am not used to VBA programming and as you can see in the code below, I don't know the right way to close an open document and exit a open Word process. If anyone could look at what I've done and suggest a way to solve this problem, it would be very helpful.</p></li>
  <li><p>I am also interested to know if this is the right way to merge several Word files. If there's a better way, please let me know.</p></li>
  </ol>
  
  <pre><code>
      'the flow:
      '  start a word process to create a blank file "combinedfile.docx"
      '  loop over all documents in "files" folder and do the following:
      '    open the file, insert it at the end of combinedfile.docx, then insert pagebreak
      '  close the file and exit the word process
  
      filesdir = ActiveWorkbook.Path + "\" + "files\"
      thisdir = ActiveWorkbook.Path + "\"
      singlefile = thisdir + "combinedfile.docx"
  
      'if it already exists, delete
      If FileExists(singlefile) Then
        SetAttr singlefile, vbNormal
        Kill singlefile
      End If
  
      Dim wordapp As Word.Application
      Dim singledoc As Word.Document
      Set wordapp = New Word.Application
      Set singledoc = wordapp.Documents.Add
      wordapp.Visible = True
      singledoc.SaveAs Filename:=singlefile
      singledoc.Close    'i do both this and the line below (is it necessary?)
      Set singledoc = Nothing
      wordapp.Quit
      Set wordapp = Nothing
  
      JoinFiles filesdir + "*.docx", singlefile
  
      Sub JoinFiles(alldocs As String, singledoc As String)
        Dim wordapp As Word.Application
        Dim doc As Word.Document
        Set wordapp = New Word.Application
        Set doc = wordapp.Documents.Open(Filename:=singledoc)
        Dim filesdir As String
        filesdir = ActiveWorkbook.Path + "\" + "files\"
  
        docpath = Dir(alldocs, vbNormal)
  
        While docpath  ""
          doc.Bookmarks("\EndOfDoc").Range.InsertFile (filesdir + docpath)
          doc.Bookmarks("\EndOfDoc").Range.InsertBreak Type:=wdPageBreak
          docpath = Dir
        Wend
        doc.Save
        doc.Close
        Set doc = Nothing
        wordapp.Quit
        Set wordapp = Nothing  
      End Sub
  </code></pre>