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:
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
no response to this
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello. I need to convert a jpg file made on paint.net, to a pdf. This is for a whole book best pdf to jpg 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
should I need to start a new thread on the same topic?
 
Upvote 0
@bimatmpmet You made multiple additional posts on a thread that was about 3 1/2 years old and was not about the same thing. Yes, you wanted to create a pdf from a jpg. However this thread was primarily about using VBA code to convert .doc to .pdf. The .jpg was an add-on, and even if it weren't this was mainly about the VBA code. So yes, if you haven't created a new thread in the past year since you posted your reply, I would highly recommend doing so.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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