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 :
Here is the code I came up for all folders (but which doesn't work), the error that is returned is the error 1004
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
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