# How to convert PNG file into PDF automatically



## m_vishal_c (Jul 17, 2017)

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


----------



## Haluk (Jul 17, 2017)

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"*


```
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
```


----------



## m_vishal_c (Jul 17, 2017)

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


----------



## Haluk (Jul 18, 2017)

You can delete the sheets created, after the PDF's are located in the destination folder.


```
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
```


----------



## Macropod (Jul 19, 2017)

m_vishal_c said:


> is it possible without creating Sheets in Excel, .PNG file will be converted into pdf .


If you have Adobe Acrobat Pro, you could use its VBA methods do the conversion.


----------

