Skip to content

Instantly share code, notes, and snippets.

@twardoch
Created August 5, 2021 12:13
Show Gist options
  • Save twardoch/4090f834c60106bac06635704ddcc75e to your computer and use it in GitHub Desktop.
Save twardoch/4090f834c60106bac06635704ddcc75e to your computer and use it in GitHub Desktop.
Convert a folder full of .pdf files into .docx and .txt using Microsoft Word for Windows
Sub ConvertPDFFolderToDOCXandTXT()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String
Dim strPath As String
Dim oDoc As Object
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
strDirectory = .SelectedItems(1)
End If
End With
If strDirectory <> "" Then
i = 1
flag = True
varDirectory = Dir(strDirectory & "\*.pdf", 0)
While flag = True
If varDirectory = "" Then
flag = False
Else
strPath = strDirectory & "\" & varDirectory
Set oDoc = Documents.Open(FileName:=strPath)
oDoc.SaveAs2 FileName:=strPath + ".docx", FileFormat:=wdFormatXMLDocument, CompatibilityMode:=15
oDoc.SaveAs2 FileName:=strPath + ".txt", FileFormat:=wdFormatText, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:=False, LineEnding:=wdLFOnly, CompatibilityMode:=0
oDoc.Close
varDirectory = Dir
i = i + 1
End If
Wend
End If
End Sub
@twardoch
Copy link
Author

twardoch commented Aug 5, 2021

This only works in recent Microsoft Word for Windows (2016 and later). Does not work in older Word, and not in Word for Mac.

To use it:

  1. Enable the Developer tab using this method.

  2. Create a macro named ConvertPDFFolderToDOCXandTXT using this method and then replace that macro’s code with the above.

  3. Run the macro, pick the folder containing PDFs and wait :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment