Excel Automation: SaveAsPdf

_Midnight__Midnight_ Member Posts: 15
Hi guys!

Hope somebody can help me with this...

I use excel Automation-obj to fill several data into a sheet. This happens not visible to the user,
in background. After data are filled in, I want to save the sheet as PDF, and then open it.

I used the vba-editor to get an idea of how to call the right function.
Result is: Excel uses "ExportAsFixedFormat"

I've tried several parameters, but the command always fails with error -2147352567.
Searching for description at google and microsoft didn't helped a lot.

Does anybody got a hint?
Or does someone already have a solution for saving excel as PDF from C/AL?
Appreciate every help!

Comments

  • _Midnight__Midnight_ Member Posts: 15
    ....additional Info:
    When I remove the Export-Command from C/AL (or comment-out), and trigger it as macro from the open Excel, it works perfect.
  • mdPartnerNLmdPartnerNL Member Posts: 802
    Could you show the code + the macro code?
  • _Midnight__Midnight_ Member Posts: 15
    Code in nav as follows....
    xlWbook := xlApp.Workbooks._Open(gTemplateFile);
    
    //Specific data
    xlSheet := xlApp.Sheets.Item(1);
    xlSheet.Range('C3').Value := gFType;
    xlSheet.Range('C4').Value := '596';
    xlSheet.Range('C5').Value := '896';
    
    xlApp.ScreenUpdating(FALSE);
    xlApp.Visible := FALSE;
    
    //Change to Printsheet, Export as PDF
    xlSheet := xlApp.Sheets.Item(6);
    xlSheet.ExportAsFixedFormat(0,'C:\exports\dataRange.pdf',0,TRUE,FALSE,'','',TRUE);                  //<--- Error thrown
    
    //Close WB
    xlWbook.close;
    xlApp.Quit;
    


    And the macro code stays quite the same:
    ActiveSheet.ExportAsFixedFormat xlTypePDF, "C:\exports\dataRange.pdf", xlQualityStandard, TRUE, FALSE, , , TRUE
    
  • mdPartnerNLmdPartnerNL Member Posts: 802
    How do you call the macro?

    Im using Excel a lot too. Maybe try to replace the explicit parameters with vars.

    So
    ''
    becomes
    LtStringEmty := '';

    0
    becomes
    LiZero := 0;

    TRUE
    becomes
    LbTRUE := true;

    etc.

    xlSheet.ExportAsFixedFormat(LiZero ,'C:\exports\dataRange.pdf',LiZero,LbTRUE,LbFALSE,LtZero,LtStringEmty,LbTRUE);

    ps.
    Whats helps too is not use integers or strings but use variants vars like LvTRUE := true;
  • _Midnight__Midnight_ Member Posts: 15
    I tried what you have suggested, but it didn't helped.
    I still get the exception....

    For now, I will call a macro with the export command.
    But I don't like doing it that way at all, cause I have to save my template with macros,
    and furthermore, edit the security settings of excel, to permit execution of it.

    The way I call the macro:
    xlApp.Run('macroName','param1','param1');
    

    thanks again
  • winfywinfy Member Posts: 8
    Try this one:
    xlSheet.ExportAsFixedFormat(0,'C:\exports\dataRange.pdf');
    

    regards,
    winfy
Sign In or Register to comment.