Likes Likes:  0
Results 1 to 9 of 9

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

  1. #1
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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:
    Sub WordtoPDF()
    Application.ScreenUpdating = False
    
    
    Dim file
    Dim path As String
    
    
    path = "U:\Documents\File Conversion Test"
    
    
    file = Dir(path & "*.doc") 'Dir means directory
    Do While file <> ""        '<> means not equal, so this says if the file name isn't blank
    Documents.Open FileName:=path & file   'Open files that are located in this directory, ending in .doc
    
    
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            file & ".pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        ChangeFileOpenDirectory "U:\Documents\File Conversion Test"
    
    
    ActiveDocument.Save   'Save active doc
    ActiveDocument.Close  'Close active doc
    
    
    file = Dir()
    
    
    Loop
    
    
    Application.ScreenUpdating = True
    End Sub
    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 Fluff; Apr 13th, 2018 at 11:42 AM. Reason: Code tags

  2. #2
    Moderator Macropod's Avatar
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    2,718
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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?

  4. #4
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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

  5. #5
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    Hello again,

    Going back to the first post - does anyone know why the code might skip the first document in the folder?

  6. #6
    Moderator Macropod's Avatar
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    2,718
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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!

  8. #8
    Moderator Macropod's Avatar
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    2,718
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  9. #9
    New Member
    Join Date
    Apr 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Code to convert .jpg to .pdf, and other conversions

    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!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •