The xlsx file is not created when I need it. Can you help me?

Hi,

I have 2 applications The first is a Master/Detail form application and the second a Blank application.

The first allows you to select records from detail and the second takes the records ​​selected by the user and creates an xlsx file using PHPExcel.

At the moment both applications works.

Now I have added one more functionality to the first application. The goal is to send an email to a specific user attaching the excel file that created the blank application.

This is already done by the application. The problem I have is that if I run the master/detail application the first time, it creates the xlsx file, but when it tries to send the email it shows an error message indicating that it does not find the file created by the blank application.

If I run the application a second time and reselect the same records, then it finds the file and does not show the error message.

Therefore I have seen that the file is created until I exit the Master/Detail form. and not at the end of the application blank.

I do not know if I can explain myself. I do not speak a lot of english.

These are basically the steps executed by the apps. [LIST=1]

  • User selects records and presses a RUN button.
  • The code on the RUN button is executed. Search the records in the database and assign them to an array.
  • The code calls the blank application and moves the array as parameter.
  • The blank application creates the xlsx file and returns to the form application (sc_exit (ref))
  • The form application creates an email and tries to attach the xlsx file.
  • Error is generated since the file does not exist (when searching the file in the folder, the file is not yet in that place)
  • When the Master/Detal application finishes it creates the excel file in the folder. [/LIST] In summary the questions are,
    • [B]Why the Excel file was created until the form application ends and not at the moment when the blank application ends?[/B]
    • [B]What do I have to do so that the excel file can be created and saved in the folder before the routine that creates and sends the mail is executed?[/B]
    All the help you can give me will be very useful.

    Thank you very much in advance

    Aldo.

  • Add the code to email with attachment in your Blank application after you have created the Excel file.

    
    
    // Output the generated excel file so that the user can save or open the file
    
    // Excel 95, 2000
    $Filename=[fname];
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$Filename.'"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5');
    
    // save it to the server directory
    $objWriter->save($Filename);
    
    // save it to the client pc directory
    $objWriter->save("php://output");
    
    
    if ([sendmsg]=="1")
    {
        /** Send an email with attachment */
        $mail_smtp_server = $BSServer;  // SMTP server name or IP address 
        $mail_smtp_user = $BSUser;  // SMTP user name 
        $mail_smtp_pass = sc_decode($BSPwd);  // SMTP password 
        $mail_from = $BSFrom;  // From email 
        $mail_to = $BSEmail; // To email 
        $mail_subject = $MsgSubj;   // Message subject 
        $mail_message = $Msgbody; // Message body 
        $mail_format = 'T'; // Message format: (T)ext or (H)tml 
        $mail_copies = $BSEcc; 
        $mail_copy_type = "CCC";
    
        $mail_attachments = array($Filename); // Send email 
        sc_mail_send($mail_smtp_server, $mail_smtp_user, $mail_smtp_pass, $mail_from, $mail_to, $mail_subject, $mail_message, $mail_format, $mail_copies, $mail_copy_type, '', '', $mail_attachments);
    }
    
        // Delete file if found
        unlink($Filename); 
    
    
    
    $phpExcel->disconnectWorksheets();
    unset($phpExcel);
    unset($objWriter);