0

Can anybody tell me how to export an excel spreadsheet to pdf using delphi code?

I know there is a command in the MS Word OLE object for exporting to PDf, but I cannot find anything about Excel.

Word To PDF Example:

Const
 wdExportFormatPDF = 17;

 WrdDoc.ExportAsFixedFormat(SaveToFile, wdExportFormatPDF);
user2952034
  • 51
  • 2
  • 9
  • @Tlama i dont want to be funny, but that makes no sense to me... – user2952034 Jan 26 '14 at 13:51
  • An example code would be much nicer:D – user2952034 Jan 26 '14 at 13:56
  • TLama is not being funny, your question was badly phrased: you were talking about Word *and* Excel. For an Excel object just use ExportAsFixedFormat as well http://msdn.microsoft.com/en-us/library/office/bb238907%28v=office.12%29.aspx – Jan Doggen Jan 26 '14 at 13:57
  • i tried oSheet.ExportAsFixedFormat('C:\temp.pdf',xlTypePDF); but with no avail, i received an undeclared identifier error – user2952034 Jan 26 '14 at 14:02
  • possible duplicate of [What is the FileType number for PDF in Excel 2007 that is needed to save a file as PDF through the API?](http://stackoverflow.com/questions/738829/what-is-the-filetype-number-for-pdf-in-excel-2007-that-is-needed-to-save-a-file) – David Heffernan Jan 26 '14 at 14:02
  • tried, didnt work @davidheffernan – user2952034 Jan 26 '14 at 14:07
  • -1 for saying "didn't work" with no more detail. You need to do better than that. Now, higher up you said that `xlTypePDF` leads to undeclared identifier. So go ahead and defined `xlTypePDF`! It has value 0: http://msdn.microsoft.com/en-us/library/office/bb241296(v=office.12).aspx All you need to do is read the error messages and do some websearch. – David Heffernan Jan 26 '14 at 14:08
  • Sory, i mean i tried making sense of that post, but i could not. the correct answer was marked as 17, and i tried using the same const as i do with word as that page describes. But it says type miss match when i call WrdDoc.ExportAsFixedFormat('C:\temp.pdf', wdExportFormatPDF); – user2952034 Jan 26 '14 at 14:12
  • i defined it Const xlTypePDF=0; and then called oSheet.ExportAsFixedFormat('C:\temp.pdf',xlTypePDF); and still got a type mismatch error @Davidheffernan – user2952034 Jan 26 '14 at 14:15
  • Well, what parameters does `ExportAsFixedFormat` expect? Don't be helpless in the face of compiler errors. Read them. It also never hurts to read the documentation (http://msdn.microsoft.com/en-us/library/office/bb238907(v=office.12).aspx) You'd find that your parameters are the wrong way round. So, yes this question really is a dupe. – David Heffernan Jan 26 '14 at 14:16
  • @davidheffernan You deserve a Gold star. You actually gave me links that i could use. thank you very much – user2952034 Jan 26 '14 at 14:24
  • browse through the links, his has explanations as to what needs to go where, and what the possibilities are for each of the options, as well as the const values for each – user2952034 Jan 26 '14 at 14:36
  • I think you just clicked the link I've provided and gave up even reading it. Never mind... – TLama Jan 26 '14 at 14:40

1 Answers1

1

The correct code is

wb.ExportAsFixedFormat(xlTypePDF, FileName);

or

sheet.ExportAsFixedFormat(xlTypePDF, FileName);

where wb is a workbook and sheet is a worksheet. If for some reason, your import library does not define xlTypePDF, it has value 0.

Some documentation links:

If you read the documentation carefully you will discover that you have been passing the parameters in the wrong order.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • where the hell do you guys find these links? i google my ass of and find nothing... – user2952034 Jan 26 '14 at 14:26
  • How do you think I got 247k rep? I'm not any good at programming, I'm just a wizard at websearch!!! ;-). And I'm only half joking. Seriously it just takes practise. – David Heffernan Jan 26 '14 at 14:29
  • just 1 more thing, Do you know how to change the background color of a cell?Just the white space, i have tried finding a way, but excel doesnt have alot of help for me. @DavidHeffernan – user2952034 Jan 26 '14 at 14:34
  • I do know how to do that. But I can't answer in a comment here. At least not this instant. – David Heffernan Jan 26 '14 at 14:41
  • @user2952034 Websearch gave me this http://stackoverflow.com/questions/9690924/set-excel-cell-backgroundcolor-from-c-sharp-winforms and http://office.microsoft.com/en-gb/excel-help/change-cell-background-color-using-vba-HA001136627.aspx and http://edn.embarcadero.com/article/10128 and http://msdn.microsoft.com/en-us/library/office/ff840499.aspx – David Heffernan Jan 26 '14 at 15:40