How to convert PNG file into PDF automatically

m_vishal_c

Board Regular
Joined
Dec 7, 2016
Messages
209
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi I have 52 .PNG files in one folder and i want to convert into pdf by macro in same folder. can anyone suggest me. thanks in advance
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Assuming your PNG files are in the folder >>>>> "C:\TestFolder"

Place the code below in a module and run the macro. After the macro ends, the PNG files will be located in the folder "C:\TestFolder"

Code:
Sub SavePNGtoPDF()
    Dim NewSheet As Worksheet
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = FSO.GetFolder("C:\TestFolder")
    For Each MyFile In MyFolder.Files
        If LCase(Right(MyFile.Name, 3)) = "png" Then
            Set NewSheet = Sheets.Add
            NewSheet.PageSetup.PaperSize = xlPaperA4
            NewSheet.Range("A1").Activate
            Set MyPic = NewSheet.Pictures.Insert(MyFolder & "\" & MyFile.Name)
            NewSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFolder & "\" & Left(MyFile.Name, Len(MyFile.Name) - 4) & ".pdf"
        End If
    Next
Set MyPic = Nothing
Set NewSheet = Nothing
Set MyFolder = Nothing
Set FSO = Nothing
End Sub
 
Upvote 0
hi Thanks a lot. much appreciated, this is excellent work. is it possible without creating Sheets in Excel, .PNG file will be converted into pdf . heaps thank in advance
 
Upvote 0
You can delete the sheets created, after the PDF's are located in the destination folder.

Code:
Sub SavePNGtoPDF()
    Dim NewSheet As Worksheet
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = FSO.GetFolder("C:\TestFolder")
    For Each MyFile In MyFolder.Files
        If LCase(Right(MyFile.Name, 3)) = "png" Then
            Set NewSheet = Sheets.Add
            NewSheet.PageSetup.PaperSize = xlPaperA4
            NewSheet.Range("A1").Activate
            Set MyPic = NewSheet.Pictures.Insert(MyFolder & "\" & MyFile.Name)
            NewSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFolder & "\" & Left(MyFile.Name, Len(MyFile.Name) - 4) & ".pdf"
            Application.DisplayAlerts = False
            NewSheet.Delete
            Application.DisplayAlerts = True
        End If
    Next
Set MyPic = Nothing
Set NewSheet = Nothing
Set MyFolder = Nothing
Set FSO = Nothing
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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