Universal Document Converter
Product Overview
Download
Purchase
Tutorials
Developer Solutions
Support Service
About fCoder Group


      Site search
   


      Video tutorial
Show tutorial


Main page>Developer Solutions>Examples>Visual Basic.NET>Excel Spreadsheets to PDF

Converting Excel Spreadsheets to PDF


'----------------------------------------------------------------------
' 1) Microsoft Excel 97 or above should be installed and activated on your PC.
'
' 2) Universal Document Converter 5.0 or above should be installed, too.
'
' 3) Open your project in Microsoft Visual Basic.NET.
'
' 4) In Visual Basic main menu press "Project->Add Reference...".
'
' 5) In "Add Reference" window go to "COM" tab and double click into "UDC 5.0 Type Library".
'
' 6) Before using this example, please read this article from Microsoft Excel 2003 knowledge base:
'    http://support.microsoft.com/kb/320369/en-us/
'----------------------------------------------------------------------

Private Sub PrintExcelToPDF(ByVal strFilePath As String)

    Dim objUDC As UDC.IUDC
    Dim itfPrinter As UDC.IUDCPrinter
    Dim itfProfile As UDC.IProfile

    Dim objXLApp As Object
    Dim itfXLBook As Object
    Dim itfXLWorksheet As Object
    Dim itfXLPageSetup As Object

    objUDC = New UDC.APIWrapper
    itfPrinter = objUDC.Printers("Universal Document Converter")
    itfProfile = itfPrinter.Profile

    ' Use Universal Document Converter API to change settings of converterd document
    itfProfile.PageSetup.FormName = "A2"
    itfProfile.PageSetup.ResolutionX = 200
    itfProfile.PageSetup.ResolutionY = 200
    itfProfile.PageSetup.Orientation = UDC.PageOrientationID.PO_LANDSCAPE

    itfProfile.FileFormat.ActualFormat = UDC.FormatID.FMT_PDF

    itfProfile.FileFormat.PDF.ColorSpace = UDC.ColorSpaceID.CS_TRUECOLOR
    itfProfile.FileFormat.PDF.Multipage = UDC.MultipageModeID.MM_MULTI

    itfProfile.Adjustments.Crop.Mode = UDC.CropModeID.CRP_AUTO

    itfProfile.OutputLocation.Mode = UDC.LocationModeID.LM_PREDEFINED
    itfProfile.OutputLocation.FolderPath = "C:\Out"
    itfProfile.OutputLocation.FileName = "&[DocName(0)] -- &[Date(0)] -- &[Time(0)].&[ImageType]"
    itfProfile.OutputLocation.OverwriteExistingFile = False

    ' Run Microsoft Excle as COM-server
    On Error Resume Next
    objXLApp = CreateObject("Excel.Application")

    ' Open spreadsheet from file
    itfXLBook = objXLApp.Workbooks.Open(strFilePath, , True)

    ' Change active worksheet settings and print it
    itfXLWorksheet = itfXLBook.ActiveSheet
    itfXLPageSetup = itfXLWorksheet.PageSetup

    itfXLPageSetup.Orientation = 2 ' Landscape

    Call itfXLWorksheet.PrintOut(, , , False, "Universal Document Converter")

    ' Close the spreadsheet
    Call itfXLBook.Close(False)
    itfXLBook = Nothing

    ' Close Microsoft Excel
    Call objXLApp.Quit()
    objXLApp = Nothing

End Sub


© 2001-2010 fCoder Group, Inc. About fCoder Group | Privacy Policy | Site Map