我有下面的函数,我想用它从Excel创建一个PDF。但是当试图保存PDF时,我得到一个OLE错误800A03EC,我根本无法弄清楚。我用同样的方法从Word创建PDF,效果很好。
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;
使用
初始化Excelfunction 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;
应该用
结束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将是一个正常的保存。您应该尝试导出它,如下所示