将 Excel 工作簿另存为 PDF 时出现 OLE 错误 800A03EC
Saving Excel workbook as PDF gives me an OLE error 800A03EC
我想使用以下函数从 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;
我使用
初始化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;
它应该使用
关闭
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 是正常的另存为。
您应该尝试导出它,as said here
我想使用以下函数从 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 是正常的另存为。 您应该尝试导出它,as said here