Friday, October 4, 2013

How to Convert an Excel File to PDF Through VB6


1. Create your connection to the Excel spreadsheet. This is done using a connection object that includes the Excel driver. The following code creates the Excel connection:Dim conn As ADODB.ConnectionDim connstring As Stringconnstring = 'Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=excelfile.xls;'conn.ConnectionString = connstring
2. Retrieve the information from the Excel worksheet. In this example, all information from the Excel spreadsheet is taken to convert the entire file to the PDF. The following code copies all the Excel data:sql = 'SELECT * FROM sheet1'excel_records = recordset.Open sql, con, , adLockOptimistic, adCmdTextReplace 'Sheet1' with the name of your spreadsheet in the file.
3. Create the PDF file. You must first initiate the PDF class and specify the file for which the content is transferred. The following code creates and opens the file:Dim pdf As New mjwPDFpdf.PDFTitle = 'Excel to PDF conversion'pdf.PDFFileName = 'converted.pdf'
4. Save the Excel information to the PDF file. The following code transfers your Excel information:pdf.PDFTextOut excel_records.ToString
5. Close the Excel and PDF files to release the resources used to open them. This is good for server performance and frees any operating system locks on the files. The following code releases the files:Set pdf = NothingSet conn = Nothing