Convert .jpg files to PDF

Wicowan

New Member
Joined
Jan 12, 2022
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hello,
I currently have thousands of folders with each of them having up to 10 .jpg image in them. My goal is to create a macro that goes over all folders, and for each of them, converts all .jpg files inside it into one PDF (1 img/page). I have successfully made the macro for one folder, so basically regrouping .jpg files into one PDF. However when I change the code to make it work for all folders, it doesn't work. Here is first the code that works for one folder :

VBA Code:
Sub JPG_PDF()
'
' JPG_PDF Macro
'
Application.ScreenUpdating = False

'Declare variables
Dim file
Dim path As String
Dim count As Integer

path = "C:\Users\Lorian\Desktop\Example_jpegALL\3\"
file = Dir(path & "*.jpg")

Debug.Print path
Sheet1.Activate


'Start loop
Do While file <> ""

Debug.Print file

count = ActiveSheet.Pictures.count
'Insert picture into Excel
With ActiveSheet.Pictures.Insert(path & file)
    .Left = count * 435
    .Top = ActiveSheet.Range("A1").Top
    .Width = 400
End With
ActiveSheet.Pictures(ActiveSheet.Pictures.count).Name = "A Picture"

count = ActiveSheet.Pictures.count
Debug.Print count

        
file = Dir()

Loop

ChDir "C:\Users\Lorian\Desktop\Example_jpegALL\PDF"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="file", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False

ActiveSheet.Pictures.Delete



Sheet2.Activate

Application.ScreenUpdating = True


End Sub

Here is the code I came up for all folders (but which doesn't work), the error that is returned is the error 1004

VBA Code:
Sub list()

Application.ScreenUpdating = False
    
    Const FPATH As String = "C:\Users\Lorian\Desktop\Example_jpegALL\"
    Dim d, coll As New Collection, file, f, folder, count As Integer
    
    coll.Add FPATH 'add the root folder
    'check for subfolders (one level only)
    d = Dir(FPATH, vbDirectory)
    Do While d <> ""
        If (GetAttr(FPATH & d) And vbDirectory) <> 0 Then
            If d <> "." And d <> ".." Then coll.Add FPATH & d
        End If
        d = Dir()
    Loop
    
    
    For Each folder In coll
        
        Sheet1.Activate
        
        file = Dir(folder & "\*.jpg")
         Debug.Print folder
        Do While file <> ""
            Debug.Print , file
            
            count = ActiveSheet.Pictures.count
            'Insert picture into Excel
            With ActiveSheet.Pictures.Insert(FPATH & file)
                .Left = count * 435
                .Top = ActiveSheet.Range("A1").Top
                .Width = 400
            End With

 
            ActiveSheet.Pictures(ActiveSheet.Pictures.count).Name = "A Picture"
            
            count = ActiveSheet.Pictures.count
            Debug.Print count
            file = Dir()
        Loop
        
        ChDir "C:\Users\Lorian\Desktop\Example_jpegALL\PDF"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
        jpg
        ActiveSheet.Pictures.Delete
        
        Sheet2.Activate
        
        Application.ScreenUpdating = True
        
    Next
End Sub

Another thing to mention is that I successfully found a way to make two loops that goes through folders and file, for the purpose of listing the name of the .jpg files, but whenever I triy to apply it, it doesn't work (the code is basically the same as the second, but without all insert things, and with some debug.Print...) Thanks for anyone reading till the end :)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I found the answer by myself, if it interests someone, please let me know
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top