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

Hello monks!

What stumps me is pretty straight forward. I need to save a Word file using Win32::OLE, but I need to replace an existing file if there is any. What properties do I have to set before calling SaveAs() to do that? Any help is much appreciated. Thank you very much.

Replies are listed 'Best First'.
Re: Win32::OLE SaveAs question.
by Gangabass (Vicar) on Sep 14, 2007 at 00:12 UTC

    You may try this way:
    before SaveAs

    application.displayalerts = false

    then after SaveAs restore original value:

    application.displayalerts = true

      Just to pick a nit: this is not restoring the original value. You need to query what the original value is before you start, and restore that after you are done. Make certain that you catch all exit points from your routine, or you may end up with some funky behavior.

      Not being a Win32:OLE user, I don't know the required calls to do it.

      --MidLifeXis

Re: Win32::OLE SaveAs question.
by Muggins (Pilgrim) on Sep 14, 2007 at 17:10 UTC

    As the previous poster said, switch off "DisplayAlerts". But if you're doing it in perl:

    use Win32::OLE; use Win32::OLE::Const; #... my $ex_ol = Win32::OLE::Const->Load("Microsoft Excel 10.0 Object Libra +ry"); my $xcl = Win32::OLE->new('Excel.Application','Quit'); #later $xcl->Application->{DisplayAlerts} = 0; $doc->SaveAs($outputFileName, $$ex_ol{'xlWorkbookNormal'});

    This worked fine for me on AS Perl 5.8.8 (an old build: 817). The reason you want to stop displaying alerts is that a dialog-box saying "Are you sure you want to overwrite..." comes up and stops the SaveAs from happening.

    This happens even if you've made the worksheet invisible, so can lead to confusing bugs with SaveAs.