use strict;
use warnings;
#use Storable;
#use Cwd;
use Win32::OLE;
#use Win32::OLE qw(in with);
#my $file = cwd.'\\'."myexcel.xlsm";
my $file = 'X:\Data\Perl\1131763\example.xls';
my $Excel = Win32::OLE->new("Excel.Application");
$Excel->{Visible} = 1;
my $workbook = $Excel->Workbooks->Open($file);
die "Failed to define Excel workbook" unless (defined $workbook);
system("attrib -r $file");
$Excel->{DisplayAlerts} = 0;
$workbook->Save;
My code is indented. The crucial lines are 13 & 14, but let's consider the rest (someone else may have a similar problem, so even if it's no use to you, it may be useful). Note that I have used code tags. It's much easier for everyone. Also, you have changed your original post without identifying what has changed. Changes are fine, but please mark them as such, otherwise people new to the thread may not understand replies made before the changes.
1: Never leave this out unless you REALLY know what you're doing.
3 & 4: I'm not using them, so they don't add to the solution.
6: You aren't using this in the code given. Unless you really need this functionality, it is best left out.
7 & 8: Just my way of doing it on my machine.
10: Use throughout development. For production code, it can be left out if you are sure you are closing all instances of Excel. Otherwise, you'll get orphaned instances eating memory (effectively a memory leak).
13: This clears the read-only attribute on the file (assuming that there are the necessary permissions and you aren't doing something improper on someone else's network).
14: This prevents the dialogue box you mention appearing. Note that there are two issues here: the read-only problem that you mentioned in your OP but not in the post I'm replying to and the problem of the dialogue box that you didn't mention in your unamended OP.
Regards,
John Davies
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.