bts974 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I write a script in Perl that create an Excel file and write a data to it. the problem is that after the script is done working and I open the Excel file and try to re-run the script its give me an error message and just after I close the file its working. what can I do to solve this problem? Thanks!

Replies are listed 'Best First'.
Re: writing to an open Excel file
by marto (Cardinal) on Sep 10, 2014 at 10:47 UTC

    Welcome, there are some issues with your post:

    "after the script is done working and I open the Excel file and try to re-run the script its give me an error message and just after I close the file its working."

    You don't show us the script, you don't tell us what the error is. It could be that you're getting an error from your perl script because it's trying to write to a file which is in use by Excel, which may explain why it works when you close the file. If this is not the case please read and understand PerlMonks for the Absolute Beginner along with How do I post a question effectively? and give us more details. If I am missing something obvious let me know.

Re: writing to an open Excel file
by AppleFritter (Vicar) on Sep 10, 2014 at 11:52 UTC

    Howdy bts974, and welcome to the Monastery!

    I can't say much without having seen your script or knowing what error message you're getting, but my first guess is that Excel is holding a lock of your file that keeps it from being written to by another application/program/script while it's open.

    The solution is to close the file in Excel before you run your script, and reopen it afterwards. (The alternative would be to file a bug report with Microsoft, but I believe that's unlikely to lead anywhere.)

Re: writing to an open Excel file
by roboticus (Chancellor) on Sep 10, 2014 at 14:14 UTC

    bts974:

    That's just how things work: Excel keeps the file open while you're working on it, and Windows isn't going to give you write access to a file another program has open with write access to. I'd suggest that you detect that the file is held open by Excel (catch the exception from eval) and either allow the user to retry later (giving the opportunity to close the spreadsheet) or accept an alternate name to save the spreadsheet to.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: writing to an open Excel file
by dasgar (Priest) on Sep 10, 2014 at 15:34 UTC

    What module(s) are you using? What application are you using to "open the Excel file"?

    If you are using one of the Spreadsheet::* modules, I'd agree with what others are saying about having the file open could be locking the file from being accessed/modified by other applications, which would include your script.

    If you're using Win32::OLE in your script to directly control Excel and you're opening the file with Excel, it is possible for your script to take control over your Excel instance to modify the contents of the file. However, I personally would not go with that approach. Instead, I would close the file and have your script open a new instance of Excel to open the file for manipulation. See Re: Manipulating open excel sheet for more details on why having your script grab control over an existing instance of Excel could be problematic.

    Regardless of which modules you use and which app you're using to open the file, I personally believe that you'll have far less issues if you don't have the file opened in any application when you run your script.