Created
August 5, 2021 12:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Enable the Developer tab using this method.
Create a macro named
ConvertPDFFolderToDOCXandTXT
using this method and then replace that macro’s code with the above.Run the macro, pick the folder containing PDFs and wait :)