Astra

Excel VBA Convert JPG to PDF

Estimated reading: 3 minutes

When you need to convert files from one format to another, VBA can provide you with one spot solution, especially for converting files from one document format to another. However, in this article, we will learn how to convert Image files(.JPG format) to document format(.PDF format)

Here we will divide the full program into four section 

Section A: Define the variables

Section B: Create a new instance of the Excel application

Section C: Access to the files in the input folder

Section D: Loop to convert all files to pdf

The full program is:

' Purpose: Convert files from JPG to PDF format and then to Word format (.docx)
Sub JPG_TO_PDF_FILE_CONVERT()
    
    ' _______________Section A: Define the variables________________________
    
    ' Variable for the input folder path
    Dim inputFolder As String
    
    ' Variable to count the number of files converted
    Dim numFilesConverted As Integer
    
    ' __________Section B:Create a new instance of Excel application________
    Dim excelApp As Excel.Application
    Set excelApp = New Excel.Application
    
    ' Make the Excel application visible
    excelApp.Visible = True
    
    '___________Section C: Access to the files in input folder_________________
    ' Get the input folder path from cell D8 in Sheet1 or you can give the folder name
    inputFolder = "C:\Users\USER\Downloads\SOLVED EXCEL\"
    Dim inputFileName As String
    inputFileName = Dir(inputFolder & "*.jpg")
    
    '_________________Section D: Loop to convert all files to pdf_______________
    ' Loop through all JPG files in the input folder and convert them to PDF
    Do While inputFileName <> ""
        
        ' Set the input and output file paths
        Dim inputFilePath As String
        inputFilePath = inputFolder & inputFileName
        
        ' Create a new Excel workbook and add the JPG file to it
        Dim excelBook As Excel.Workbook
        Set excelBook = excelApp.Workbooks.Add
        excelBook.Sheets(1).Pictures.Insert(inputFilePath).Select
        
        ' Export the workbook as a PDF file
        excelBook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=inputFilePath
        
        ' Close the workbook and release the object
        excelBook.Close False
        Set excelBook = Nothing
        
        ' Increase the count of converted files
        numFilesConverted = numFilesConverted + 1
        
        ' Move to the next file
        inputFileName = Dir
        
    Loop
    
    ' Quit the Excel application and release the object
     excelApp.Quit
     Set excelApp = Nothing
     
     ' Display the message in cell D13 of Sheet1 with the number of files converted
    ThisWorkbook.Sheets("Sheet1").Range("D13").Value = numFilesConverted & " files have been converted into PDF"
    
End Sub

Note: Before running the code check the references 

Excel VBA Convert JPG to PDF1

Result: After running the code all jpg files will be saved in pdf format in the same directory as the pdf files.

Excel VBA Convert JPG to PDF

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc
Jump to Topics
SOLVED EXCEL
Scroll to Top