Cheers everyone,
Does anyone know how to use code to automatically convert all jpg files and pdf files from a specific folder into 1 single pdf file ( with page marginalization - keeping the same width as the first page )
Thanks
God Bless
Cheers everyone,
Does anyone know how to use code to automatically convert all jpg files and pdf files from a specific folder into 1 single pdf file ( with page marginalization - keeping the same width as the first page )
Thanks
God Bless
I have used VBA to combine multiple PDFs to a single PDF. This is a rather common topic. Google "VBA merge PDF". I don't know if 'marginalization' can be programmatically managed. What should happen if some documents are in landscape orientation?
I know nothing about converting JPG to PDF.
Last edited by June7; 01-30-2024 at 09:15 PM.
How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
you can use ImageMagick?
ImageMagick – Download
Private Sub tt() Const conType As String = "*.jpg" Dim col As New Collection Dim sFile As String, j As Integer Dim path As String, ext As String ' put the PATH of the jpgs here! path = Environ$("userprofile") & "\documents\" sFile = Dir$(path & conType) Do Until Len(sFile) = 0 j = j + 1 col.Add Item:=sFile, Key:=j & "" sFile = Dir$ Loop For j = 1 To col.Count Call ConvertJpgToPdfUsingMagick(col(j), Replace$(col(j), ".jpg", ".pdf")) Next End Sub Sub ConvertJpgToPdfUsingMagick(ByVal inputImagePath As String, ByVal outputPdfPath As String) 'Dim inputImagePath As String 'Dim outputPdfPath As String Dim command As String ' Set the paths 'inputImagePath = "C:\Path\To\Input\Image.jpg" 'outputPdfPath = "C:\Path\To\Output\Output.pdf" ' Build the command command = "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe convert """ & inputImagePath & """ """ & outputPdfPath & """" ' Execute the command Shell command, vbHide End Sub
Put code in Access VBA general module.
How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Thanks June7
How can i do that ?
This is what I get when I open Access
2024-02-02 04_36_18-.jpg
here is a demo.
on the Ribbon->View->Macro->Edit: subTestMagick
when you are in VBA, press F5 to run the code.
Thanks a lot again for your reply.
I think i didnt explain quite well what i try to do. Sorry.
When i run the macro with F5 from Excel it does nothing.... i think i mess something in there
So, i have this folder C:\MIHAI\DOC\ASIG\DOSARE
in this folder will be various jpg and pdf files that i want to convert to 1 single pdf .
2024-02-03 06_24_52-DOSARE.jpg
The output folder i want to be the same folder as the input one.
So I modified your code but I think i did something wrong
Option Explicit
Sub subTestMagick()
Const conType As String = "*.jpg"
Dim col As New Collection
Dim sFile As String, j As Integer
Dim path As String, ext As String
' change the path to the correct path of your jpg's
' right now it will check all jpgs in Document folder.
path = "C:\MIHAI\DOC\ASIG\DOSARE"
sFile = Dir$(path & conType)
Do Until Len(sFile) = 0
j = j + 1
col.Add Item:=sFile, Key:=j & ""
sFile = Dir$
Loop
For j = 1 To col.Count
Call ConvertJpgToPdfUsingMagick(col(j), Replace$(col(j), ".jpg", ".pdf"))
Next
End Sub
Sub ConvertJpgToPdfUsingMagick(ByVal inputImagePath As String, ByVal outputPdfPath As String)
'Dim inputImagePath As String
'Dim outputPdfPath As String
Dim command As String
' Set the paths
inputImagePath = "C:\MIHAI\DOC\ASIG\DOSARE\Image.jpg"
outputPdfPath = "C:\MIHAI\DOC\ASIG\DOSARE\Output.pdf"
' Build the command
command = "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe convert """ & inputImagePath & """ """ & outputPdfPath & """"
' Execute the command
Shell command, vbHide
End Sub
Sorry I am such a newbie at this.
Your help is greatly appreciated.
Thanks.
do not Modify the sub ConvertJpgToPdfUsingMagick.
only modify inPath or outPath on Sub subTestMagick.
Thanks a lot ArnelGP for your patience with me. I did it and it worked but only converts the jpg not the pdf. So i have 8 jpgs and 2 pdf files in the folder and it creates a pdf file only with the 8 jpgs files not also with the 2 pdfs.
I used
inPath = "C:\MIHAI\DOC\ASIG\DOSARE\X\*.*"
outPath = "C:\MIHAI\DOC\ASIG\DOSARE\X\Output.pdf"
Thanks a lot again.
Hi again!
Is there a way to modify the code so that it deletes all the initial jpgs after the conversion is finished ?
Have a great day!
God Bless !
Below the for loop that calls the convert code. You could check for the existence of the converted PDF file(s), if it's there then delete the image file the PDF was created from:
' delete the images that have been converted to PDFs For j = 1 To col.Count If Dir("C:\MIHAI\DOC\ASIG\DOSARE\X\" & Replace$(col(j), ".jpg", ".pdf")) <> "" Then Kill "C:\MIHAI\DOC\ASIG\DOSARE\X\" & col(j) End If Next
Hi jdelano and thanks for your reply
i didnt upgraded my correct code, sorry
so I use this code to convert all jpg into 1 single pdf
So, after i run this code with a macro, I need to delete the initial pdf files.Option Explicit Public Sub subTestMagick() Dim inPath As String Dim outPath As String inPath = "C:\MIHAI\DOC\ASIG\DOSARE\*.*" outPath = "C:\MIHAI\DOC\ASIG\DOSARE\Outpu.pdf" Call ConvertJpgToPdfUsingMagick(inPath, outPath) End Sub Sub ConvertJpgToPdfUsingMagick(ByVal inputImagePath As String, ByVal outputPdfPath As String) Dim command As String command = "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe convert -resize 1240x1753 -extent 1240x1753 -gravity center -units PixelsPerInch -density 150x150 """ & inputImagePath & """ """ & outputPdfPath & """" Shell command, vbHide End Sub
How can I adapt your code to this ?
Thanks,
God Bless!
try this on a sample folder.
Public Sub subTestMagick()Dim inPath As String Dim outPath As String Dim Path As String Dim dictFiles As Object, i As Integer Set dictFiles = CreateObject("scripting.dictionary") Path = "C:\MIHAI\DOC\ASIG\DOSARE\" inPath = Path & "*.*" outPath = Path & "Output.pdf" 'save all the filename first so we can delete later Dim sFile As String sFile = Dir$(inPath) Do Until Len(sFile) = 0 dictFiles(Path & sFile) = 1 sFile = Dir$ Loop 'remove Output.pdf Select Case dictFiles.Count Case Is > 1 If dictFiles.Exists(outPath) Then 'delete only if there are other files dictFiles.Remove outPath Kill outPath End If Case Is = 1 If dictFiles.Exists(outPath) Then Exit Sub End If Case Is = 0 Exit Sub End Select 'convert to pdf Call ConvertJpgToPdfUsingMagick(inPath, outPath) 'delete the initial files For i = 0 To dicFiles.Count - 1 Kill dictFiles.Keys()(i) Next End Sub Sub ConvertJpgToPdfUsingMagick(ByVal inputImagePath As String, ByVal outputPdfPath As String) Dim command As String command = "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe convert -resize 1240x1753 -extent 1240x1753 -gravity center -units PixelsPerInch -density 150x150 """ & inputImagePath & """ """ & outputPdfPath & """" Shell command, vbHide End Sub
Hi ArnelGP
Thanks again for taking your time with me...
Its really curious what is happening here
So, if i use it like this it creates the pdf file from jpgs and then deletes all the files including the resulting pdf file:
'delete the initial files For i = 0 To dictFiles.Count - 1 Kill dictFiles.Keys()(i) Next
If i replace -1 with -2 it keeps the resulting pdf file (output.pdf ) and keeps 1 jpg file ( the last jpg file ).
For example if i have 1.jgp, 2.jpg, 3.jpg, 4.jpg it creates the file Output.pdf and keeps 4.jpg ( the last jpg file ).
'delete the initial files For i = 0 To dictFiles.Count - 2 Kill dictFiles.Keys()(i) Next