VBA Code to convert .jpg to .pdf, and other conversions

Nicole Marie

New Member
Joined
Apr 13, 2018
Messages
7
Hello!

We are taking a lot of time to manually convert different file types to PDF to be uploaded into an imaging system. I have this code to convert .doc to .pdf, and it's working:

Code:
[FONT=courier new]Sub WordtoPDF()[/FONT]
[FONT=courier new]Application.ScreenUpdating = False[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]Dim file[/FONT]
[FONT=courier new]Dim path As String[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]path = "U:\Documents\File Conversion Test"[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]file = Dir(path & "*.doc") 'Dir means directory[/FONT]
[FONT=courier new]Do While file <> ""        '<> means not equal, so this says if the file name isn't blank[/FONT]
[FONT=courier new]Documents.Open FileName:=path & file   'Open files that are located in this directory, ending in .doc[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]    ActiveDocument.ExportAsFixedFormat OutputFileName:= _[/FONT]
[FONT=courier new]        file & ".pdf", _[/FONT]
[FONT=courier new]        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _[/FONT]
[FONT=courier new]        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _[/FONT]
[FONT=courier new]        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _[/FONT]
[FONT=courier new]        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _[/FONT]
[FONT=courier new]        BitmapMissingFonts:=True, UseISO19005_1:=False[/FONT]
[FONT=courier new]    ChangeFileOpenDirectory "U:\Documents\File Conversion Test"[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]ActiveDocument.Save   'Save active doc[/FONT]
[FONT=courier new]ActiveDocument.Close  'Close active doc[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]file = Dir()[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]Loop[/FONT]
[FONT=courier new]
[/FONT]
[FONT=courier new]Application.ScreenUpdating = True[/FONT]
[FONT=courier new]End Sub[/FONT]

However, sometimes it doesn't convert the first .doc file in the folder. Any idea why?

In addition, how can I modify this code to convert .JPG to .PDF? I've done a lot of searching and I've found a couple (where the code inserts the picture into an excel doc and then saves it as PDF) but I need it to go in and find all the .jpgs in a given folder, like the one above, and convert. Lastly, can I incorporate that into the existing code (which I currently have in Word VBA), or would I have to make a new one? Would it exist in Word, or would I have to put that one in Excel? I am very new to VBA and am trying to learn (I've been searching forums and even checked out a book at the library to help me understand/help me read the coding), so any help for this newbie is truly appreciated!

Thank you.
 
Last edited by a moderator:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Here is some more efficient code for the document conversion; simply select the folder to process al all documents in that folder will be converted.
Code:
Sub DocToPDF()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      .SaveAs FileName:=strFolder & Split(strFile, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
As for converting jpg files to PDF, you would need to use an application that can read such files and be automated; Word doesn't read jpg files. The most you could do with Word is insert the jpg files into one or more documents and save those as PDFs. Doing so is liable to rescale the jpg files to fit whatever page dimensions Word is using, so that's probably not going to be acceptable.
 
Upvote 0
Hi Paul,

Thank you for the good information! I assume there aren't any programs (between Word, Excel and Access) that can read .jpg, right? If I did want to insert these into Word or Excel files and save as PDF, how would I go about writing that code? I can record myself adding the .jpg into the program and saving it as PDF, but I'm mostly wondering how to write the loop to grab multiple .jpg files, one at a time, from one directory?
 
Upvote 0
I figured this out, thank you for the help! Here is the code I came up with, in case anyone else needs it - it just takes all the .jpgs and inserts them into Excel, then saves the worksheet as a PDF:

Sub JPG_PDF()
'
' JPG_PDF Macro
'
Application.ScreenUpdating = False


'Declare variables
Dim file
Dim path As String


path = "C:\Documents\File Conversion Test"
file = Dir(path & "*.jpg")


Sheet1.Activate


'Start loop
Do While file <> ""






'Insert picture into Excel
Sheet1.Pictures.Insert (path & file)
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).name = "A Picture"

ChDir "C:\Documents\File Conversion Test"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False

ActiveSheet.Shapes.Range(Array("A Picture")).delete


file = Dir()

Loop


Sheet2.Activate

Application.ScreenUpdating = True


End Sub
 
Upvote 0
Hello again,

Going back to the first post - does anyone know why the code might skip the first document in the folder?
 
Upvote 0
I doubt the code is missing anything. More likely, you have two or more documents with the same name (but different extensions) and, as one is processed, it overwrites any of the same name with the PDF extension that already exist.
 
Upvote 0
Hi Paul,

Thanks for the idea. I've tried it with a few different samples and it doesn't look like I have any documents with the same name. Do you have any other thoughts?

Thanks again!
 
Upvote 0
Perhaps the file concerned has some form of protection (e.g. password to open) or has unreadable content. There's nothing about the code I posted that would allow a file to be skipped unless it's the file the macro is being run from - your own code doesn't even have that restriction - but would also terminate as soon as it processes itself.
 
Upvote 0
Thanks Paul for your help, I never figured out what was causing the issue (nothing had password protection, unreadable content, etc.), but your code works fine, so I just gave up and switched it. :)

Thanks again!
 
Upvote 0
Hello. I need to convert a jpg file made on paint.net, to a pdf. This is for a whole book cover to be uploaded to Kindle, so, a bit more than 2 pages wide. This is not a standard pdf format, so when I convert and upload the PDF, the cover dimensions change too much smaller.

KIndle demands the cover be uploaded as a PDF. I definitely got the JPG dimensions right because Kindle provides a template for the cover.

Solution?

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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