title: Loop over PDF files and transform them into doc with word
body: <p>I am trying to use VBA coding - which I am pretty new to - to obtain a series of .doc documents from PDFs (which are not images), that is, I am trying to loop over various PDF files and save them in MS Word format. My experience is that word reads pretty well the PDF documents that I have: word maintains the correct layout of the PDF file most of the time. I am not sure if this is the right choice to tackle this and I ask for an alternative suggestion -- using R, if possible.</p>
  
  <p>Anyway, here it is the code which I found <a href="https://social.msdn.microsoft.com/Forums/office/en-US/2d3a5cd3-38c3-4811-b3df-4f60848aec54/can-i-use-vba-to-convert-pdf-files-to-word-docs-or-text-files?forum=worddev" rel="noreferrer">here</a>:</p>
  
  <pre><code>Sub convertToWord()
  
     Dim MyObj As Object, MySource As Object, file As Variant
  
     file = Dir("C:\Users\username\work_dir_example" &amp; "*.pdf") 'pdf path
  
     Do While (file &lt;&gt; "")
  
     ChangeFileOpenDirectory "C:\Users\username\work_dir_example"
  
            Documents.Open Filename:=file, ConfirmConversions:=False, ReadOnly:= _
          False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
          "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
          Format:=wdOpenFormatAuto, XMLTransform:=""
  
      ChangeFileOpenDirectory "C:\Users\username\work_dir_example"
  
      ActiveDocument.SaveAs2 Filename:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
          , LockComments:=False, Password:="", AddToRecentFiles:=True, _
          WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
           SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
          False, CompatibilityMode:=15
  
      ActiveDocument.Close
  
       file = Dir
  
     Loop
  
  End Sub
  </code></pre>
  
  <p>After pasting it in the developer's window, I save the code in a module -> I close the developer's window -> I click on the "Macros" button -> I execute the "convertToWord" macro. I get the following error in a pop up box: "Sub or Function not defined". How do I fix this? Also, previously, for some reason that is not clear to me now, I got an error related to the function <code>ChangeFileOpenDirectory</code>, which seemed not to be defined also.   </p>
  
  <p><strong>Update 27/08/2017</strong></p>
  
  <p>I changed the code to the following:</p>
  
  <pre><code>Sub convertToWord()
  
     Dim MyObj As Object, MySource As Object, file As Variant
  
     file = Dir("C:\Users\username\work_dir_example" &amp; "*.pdf")
  
     ChDir "C:\Users\username\work_dir_example"
  
     Do While (file &lt;&gt; "")
  
          Documents.Open Filename:=file, ConfirmConversions:=False, ReadOnly:= _
          False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
          "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
          Format:=wdOpenFormatAuto, XMLTransform:=""
  
          ActiveDocument.SaveAs2 Filename:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
          , LockComments:=False, Password:="", AddToRecentFiles:=True, _
          WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
           SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
          False, CompatibilityMode:=15
  
      ActiveDocument.Close
  
       file = Dir
  
     Loop
  
  End Sub
  </code></pre>
  
  <p>Now I do not get any error messages in a pop up box, but there is no output in my working directory. What might be wrong with it right now?</p>