Closed and save PDF from Excel VBA
I use the following code to convert an excel file to PDF.
Is it possible to save the PDF and closed it from VBA and not from the dialog box?
The code leads to the SaveAs dialog box.
The problem is that i use the code in a loop to save many excel files in a folder as pdf.
Excel must not closed because contains the addresses of the excel workbooks that i intented to convert to PDF.
for example:
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3631.xls
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3635.xls
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3644.xls
Sub ExcelToPDF()
Dim iPtr As Long
Dim sFileName As String
iPtr = InStrRev(ActiveWorkbook.FullName, ".")
If iPtr = 0 Then
sFileName = ActiveWorkbook.FullName & ".pdf"
Else
sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".pdf"
End If
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="PDF Files (*.pdf), *.pdf")
If sFileName = "False" Then Exit Sub
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFileName, Quality:=xlQualityStandard, openAfterPublish:=False
End Sub
I use the following code to convert an excel file to PDF.
Is it possible to save the PDF and closed it from VBA and not from the dialog box?
The code leads to the SaveAs dialog box.
The problem is that i use the code in a loop to save many excel files in a folder as pdf.
Excel must not closed because contains the addresses of the excel workbooks that i intented to convert to PDF.
for example:
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3631.xls
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3635.xls
C:\Users\sp\Desktop\CombineFolder - 20131211 - 1912\04 10 2013 3644.xls
Sub ExcelToPDF()
Dim iPtr As Long
Dim sFileName As String
iPtr = InStrRev(ActiveWorkbook.FullName, ".")
If iPtr = 0 Then
sFileName = ActiveWorkbook.FullName & ".pdf"
Else
sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".pdf"
End If
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="PDF Files (*.pdf), *.pdf")
If sFileName = "False" Then Exit Sub
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFileName, Quality:=xlQualityStandard, openAfterPublish:=False
End Sub