Hi @Shehzad Shoukat ,
You can consder using Workbook.ExportAsFixedFormat Method to export Excel documents in PDF format.
Imports Microsoft.Office.Interop.Excel
Public Class ExcelToPDFExporter
Public Sub ExportToPDF(ByVal excelFilePath As String, ByVal pdfSavePath As String)
Dim excelApp As New Application()
Dim excelWorkbook As Workbook = excelApp.Workbooks.Open(excelFilePath)
' Export the workbook to PDF
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfSavePath)
' Close the workbook and quit Excel application
excelWorkbook.Close(False)
excelApp.Quit()
' Release COM objects to avoid memory leaks
ReleaseComObject(excelWorkbook)
ReleaseComObject(excelApp)
MessageBox.Show("Excel workbook has been exported to PDF successfully.")
End Sub
Private Sub ReleaseComObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
MessageBox.Show("Exception Occurred while releasing object " & ex.ToString())
Finally
GC.Collect()
End Try
End Sub
End Class
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.