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


      Site search
   


      Popular conversions
PDF to JPG converter
Convert Word to PDF
Convert PDF to TIFF
Autocad to PDF converter
Convert Word to JPG
Powerpoint to PDF converter
Printing to PDF
Convert Excel to PDF
Convert DJVU to PDF
Convert Web Page to PDF

      Video tutorials
Show tutorial



Main page>Developer Solutions>Examples>Visual C++>Excel Spreadsheets to PDF

Converting Excel Spreadsheets to PDF for Visual C++


//////////////////////////////////////////////////////////////////
// This example was designed for using in Microsoft Visual C++ from 
// Microsoft Visual Studio 2003 or above.
//
// 1. Microsoft Excel 97 or above should be installed and activated on your PC.
//
// 2. Before using this example, please read this article from Microsoft Excel 2003 knowledge base:
//    http://support.microsoft.com/kb/320369/en-us/
//
// 3. Universal Document Converter 5.2 or above should be installed, too.
//
// 4. You must initialize the COM before you call any COM method.
// Please insert "::CoInitialize(0);" in your application initialization
// and "::CoUninitialize();" before closing it.
//
// 5. Import Office libraries for 32-bit version of Windows.
// For 64-bit version please change "C:\\Program Files\\" to
// "C:\\Program Files (x86)\\" in all pathes.
#pragma message("Import MSO.DLL")
// MS Office 2000 -> "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE10\\MSO.DLL"
// MS Office 2003 -> "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\MSO.DLL"
// MS Office 2007 -> "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL"
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
	rename_namespace("MSO"), auto_rename
#pragma message("Import VBE6EXT.OLB")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB" \
    rename_namespace("VBE6EXT")
#pragma message("Import MS Excel API")
// MS Office 2000 -> "C:\\Program Files\\Microsoft Office\\OFFICE\\Excel9.OLB"
// MS Office 2003 -> "C:\\Program Files\\Microsoft Office\\OFFICE11\\Excel.EXE"
// MS Office 2007 -> "C:\\Program Files\\Microsoft Office\\OFFICE12\\Excel.EXE"
#import "C:\\Program Files\\Microsoft Office\\OFFICE12\\Excel.EXE"\
    rename_namespace("MSEXCEL"), auto_rename
// 6. Import Universal Document Converter software API:
#import "progid:udc.apiwrapper" rename_namespace("UDC")
//////////////////////////////////////////////////////////////////
static COleVariant vTrue( (short)TRUE ), vFalse( (short)FALSE ), vOpt( (long)DISP_E_PARAMNOTFOUND, VT_ERROR );
void PrintExcelToPDF( CString sFilePath )
{
  UDC::IUDCPtr pUDC(__uuidof(UDC::APIWrapper));
  UDC::IUDCPrinterPtr itfPrinter = pUDC->Printers["Universal Document Converter"];
  UDC::IProfilePtr itfProfile = itfPrinter->Profile;
// Use Universal Document Converter API to change settings of converterd document
  itfProfile->PageSetup->ResolutionX = 600;
  itfProfile->PageSetup->ResolutionY = 600;
 
  itfProfile->FileFormat->ActualFormat = UDC::FMT_PDF;
 
  itfProfile->FileFormat->PDF->ColorSpace = UDC::CS_TRUECOLOR;
  itfProfile->FileFormat->PDF->Multipage = UDC::MM_MULTI;
 
  itfProfile->OutputLocation->Mode = UDC::LM_PREDEFINED;
  itfProfile->OutputLocation->FolderPath = L"C:\\Out";
  itfProfile->OutputLocation->FileName = L"&[DocName(0)] -- &[Date(0)] -- &[Time(0)].&[ImageType]";
  itfProfile->OutputLocation->OverwriteExistingFile = FALSE;
  itfProfile->PostProcessing->Mode = UDC::PP_OPEN_FOLDER;
// Run Microsoft Excel as COM-server
  MSEXCEL::_ApplicationPtr itfXLApp(L"Excel.Application");
  MSEXCEL::_WorkbookPtr itfXLBook;
  MSEXCEL::_WorksheetPtr itfXLWorksheet;
  MSEXCEL::PageSetupPtr itfXLPageSetup;
// Open the document from a file
  itfXLBook = itfXLApp->Workbooks->Open( (LPCTSTR)sFilePath, vOpt, vTrue );
// Change active worksheet settings and print it
  itfXLWorksheet = itfXLBook->ActiveSheet;
  itfXLPageSetup = itfXLWorksheet->PageSetup;
  itfXLPageSetup->Orientation = MSEXCEL::xlLandscape;
  
  itfXLWorksheet->PrintOut( vOpt, vOpt, vOpt, vFalse, "Universal Document Converter");
  // Close the spreadsheet without saving changes
  itfXLBook->Close( vFalse );
  
  // Close Microsoft Excel
  itfXLApp->Quit();
}


© fCoder SIA About fCoder SIA | Privacy Policy | Site Map