Search code examples
exceldelphiole

Excel Automation 'FitToPagesWide' not supported by automation object


I'm trying to export an Excel sheet to PDF and I'd like the sheet to fit within the width of the page. In doing some research, it seems that I should use FitToPagesWide, however, I'm unable to find Delphi examples.. Here is my code:

procedure TForm1.ExcelToPDF(const AFilename: string);
Const
  XlTypePDF = 0;
  xlQualityStandard = 0;
  IncludeDocProperties = true;
  IgnorePrintAreas = false;
  LandscapeMode = 2;
var
  P: OleVariant;
begin
  P:= CreateOleObject('Excel.Application');
  try
    P.WorkBooks.Open(AFilename, 3);
    p.ActiveSheet.Pagesetup.Orientation := LandscapeMode;   orientation
    p.ActiveSheet.PageSetup.FitToPagesWide := 1;   // <-- not supported by automation object
    p.ActiveWorkbook.exportAsFixedFormat(xlTypePDF,
                                         AFilename,
                                         xlQualityStandard,
                                         IncludeDocProperties,
                                         IgnorePrintAreas );
  finally
    p.Quit;
  end;

Solution

  • Seems that setting the Zoom and FitToPagesTall properties before setting FitToPagesWide works.

    p.ActiveSheet.PageSetup.Zoom := false;
    P.Activesheet.Pagesetup.FitToPagesTall := 1;
    p.ActiveSheet.PageSetup.FitToPagesWide := 1;