保存Excel工作簿作为PDF给我一个OLE错误800A03EC

我有以下function,我想用于从Excel中创buildPDF。 但是,当试图保存PDF时,我得到一个OLE错误800A03EC,我根本无法弄清楚。 我使用相同的方法来从Word创buildPDF,并且工作得很好。

function xlsCreatePdf(aInput: string; aOutput: string = ''): string; var FileDoc: OleVariant; FilePDF: OleVariant; ExcelBook: ExcelWorkbook; PdfName: string; ExcelApp: ExcelApplication; begin Result := ''; if aOutput = '' then begin PdfName := Folders.DirOutput + ExtractFileName(aInput); PdfName := ChangeFileExt(PdfName, '.pdf'); end else PdfName := aOutput; if (Files.Validate(aInput)) then begin if not Assigned(ExcelApp) then ExcelApp := xlsInitialize; FileDoc := aInput; ExcelBook := ExcelApp.Workbooks.Open(FileDoc, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, DefaultLCID); FilePdf := PdfName; ExcelBook.SaveAs(FilePdf, xlTypePDF, EmptyParam, EmptyParam, False, False, xlNoChange, xlUserResolution, False, EmptyParam, EmptyParam, EmptyParam, DefaultLCID); xlsFreeAndNil(ExcelApp); if (Files.Validate(PdfName)) then Result := PdfName else Result := ''; end; end; 

我使用初始化Excel

 function xlsInitialize: ExcelApplication; var ExcelApp: ExcelApplication; begin DefaultLCID := LOCALE_USER_DEFAULT; ExcelApp := CreateOleObject('Excel.Application') as ExcelApplication; if Assigned(ExcelApp) then begin Result := ExcelApp; ExcelApp.Visible[DefaultLCID] := True; end else raise Exception.Create('Cannot initialize Excel application'); end; 

它应该被closures使用

 procedure xlsFreeAndNil(var ExcelApp: ExcelApplication); var i: integer; begin if Assigned(ExcelApp) then begin for i := 0 to ExcelApp.WorkBooks.Count - 1 do ExcelApp.WorkBooks.Close(DefaultLCID); ExcelApp.Quit; Pointer(ExcelApp) := Nil; end; end; 

我正在使用Delphi XE7和Excel 2010 – 所使用的Office TLB是2010年

我不认为转换为PDF将是一个正常的SaveAs。 正如这里所说的 ,你应该尝试导出它