in reply to Re: Converting Excel to Pdf
in thread Converting Excel to Pdf

Many thanks for that.
I do have a PDF printer driver so that using Win32::OLE seems an excellent idea.
I have looked at the link you sent.
However, I could not see an obvious way of using this or a reference to printing.
Therefore any clues about how Win32::OLE can be used will be more than welcome!

Replies are listed 'Best First'.
Re^3: Converting Excel to Pdf
by Corion (Patriarch) on Jun 15, 2009 at 08:25 UTC

    I gave you several links, and all of them mean you will have to do some work yourself. Printing to PDF is easiest done by following the instructions of Use The Macro Recorder, Luke (Re: Using Win32::OLE and Excel - Tips and Tricks), as with any automation of Microsoft Office products. I wish that OpenOffice had the same facility, but as OpenOffice is more oriented towards you wading through the horrendous Java API than towards you getting things done by showing it an example and then parametrizing that example, that'll remain a pipe dream I guess.

      I have just tried using the macro recorder. It gave
      Application.ActivePrinter = "deskPDF on DDM:" ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,""deskPDF on DDM:"",,TRUE +,,FALSE)"
      I have done this before but always struggle partly because I have no experience of VBA.
      I suspect that Application.ActivePrinter becomes something like Applciation->ActivePrinter but I am not sure what should come before this.
      I am not all sure how to change the Execute line.
      Finally, although I did give a file name before I stopped the macro I could not see this in the recorded code.
      I do mind at all do the work it is just the way to go about it is a bit obscure.

        It seems that your PDF printer driver asks you for a filename. That's bad, as you can't (easily/conveniently) remote control that input.

        It's weird that the macro recorder records a call to ExecuteExcel4Macro - in my opinion, it should have recorded a call to ActiveWorksheet.PrintOut. But maybe you pressed a special "Print to PDF" button or something instead of pressing the generic "Print" button in Excel?

        Translating from VB to Win32::OLE is fairly easy, but you'll have to learn a bit of VB or be willing to experiment a bit. Start with:

        my $excel = Win32::OLE->new('Excel.Application'); my $workbook = $excel->Workbooks->Add($absolute_path_to_excel_filename +); my $first_worksheet = $workbook->Sheets(1); $first_worksheet->PrintOut();

        Then, you'll have to look at what parameters you're allowed to pass to ->PrintOut. These are documented in the MSDN or the Office Object Model Documentation (available when you press F2 to get the object browser and then see the documentation of the methods of the Workbook object. Also see WIN32, OLE, Excel, and Printing which might have more code and information.