How to Convert an Excel File to PDF Through VB6

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert an Excel File to PDF Through VB6
Convert your spreadsheets to PDF. (number background image by kuhar from

Adobe PDF files are protected documents you can distribute to readers on the Internet. You can convert several file types to PDF to protect your work from being copied or edited. These files can be converted on-the-fly using Visual Basic programming. This is beneficial when you have documents on your web server that you want to protect and distribute online. Visual Basic has internal libraries used to convert your Excel file to PDF.

Skill level:


  1. 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 con As ADODB.Connection

    Dim connstring As String

    connstring = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=excelfile.xls;"

    conn.ConnectionString = connstring

  2. 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 sheet 1"

    excel_records = recordset.Open sql, con, , adLockOptimistic, adCmdText

    Replace "Sheet1" with the name of your spreadsheet in the file.

  3. 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 mjwPDF

    pdf.PDFTitle = "Excel to PDF conversion"

    pdf.PDFFileName = "converted.pdf"

  4. 4

    Save the Excel information to the PDF file. The following code transfers your Excel information:

    pdf.PDFTextOut excel_records.ToString

  5. 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 = Nothing

    Set conn = Nothing

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.