Astra

VBA Code to Convert PDF to WORD File

Estimated reading: 2 minutes

The following code will convert all pdf files in a specified folder  to the Word file 

'Purpose: convert all pdf files in a specified folder  to the Word file
Sub PDF_TO_WORD_FILE_CONVERT()

    ' Define variables
    
    ' Variable for the path of the folder containing the PDF files
    Dim pdf_folder_path As String
    
    ' FileSystemObject to access the computer's file system
    Dim fso As New FileSystemObject
    Dim folder_obj As folder
    Dim file_obj As File
    
    ' Assign the value of cell D8 to the pdf_folder_path variable
    pdf_folder_path = "C:\Users\USER\Downloads\SOLVED EXCEL"
    
    ' Get the folder object corresponding to the pdf_folder_path variable
    Set folder_obj = fso.GetFolder(pdf_folder_path)
    
    ' Create an Application object(Word)
    Dim word_app As Object
    Dim document_obj As Object
    Set word_app = CreateObject("word.application")
    word_app.Visible = True
    
    ' Variable to count the number of files converted
    Dim files_converted As Integer
    files_converted = 0
    
    ' Loop through each file in the pdf_folder_path
    For Each file_obj In folder_obj.Files
        
        ' Open the PDF file and create a document object
        Set document_obj = word_app.Documents.Open(file_obj.Path)
        
        ' Save the document object in the same directory of the PDF file with a .docx extension
        document_obj.SaveAs2 (pdf_folder_path & "\" & Replace(file_obj.Name, ".pdf", ".docx"))
        document_obj.Close False
        
        ' Increment the files_converted variable
        files_converted = files_converted + 1
    Next
    
    ' Quit Word
    word_app.Quit
    
    ' Display the number of files converted in cell D13
    Range("D13").Value = files_converted & " files have been converted into Word"
    
End Sub

Note: Please check the following references before running the code

VBA Code to Convert PDF to WORD File

Result: The code will convert all files in the folder (C:\Users\USER\Downloads\SOLVED EXCEL) to Word file format(.docx)

  1. Run the code by pressing F5
  2. Click on OK if the following notification will come in Word Application 
VBA Code to Convert PDF to WORD File1
  1. The PDF file will open in the Word application and will be saved in the same folder.
VBA Code to Convert PDF to WORD File3
Loading comments...
Share this Doc
Jump to Topics
SOLVED EXCEL
Scroll to Top