Devel-PerlySense

 view release on metacpan or  search on metacpan

t/data/simple-lib/lib/Win32/Word/Writer.pm  view on Meta::CPAN

#    $self->oWord->{ScreenUpdating } = 0;        #Makes it faster. Visible = 0 would make it faster, but doesn't work for all operations. See also: http://word.mvps.org/FAQs/InterDev/MakeAppInvisible.htm
    $self->oWord->{DisplayAlerts} = $rhConst->{wdAlertsNone};            #Suppress dialog boxes. Doesn't work that well though...
    return;
}





=head2 Open($file)

Discard the current document and open the Word document in
$file.

Note that you may want to MoveToEnd() after opening an existing
document before adding new text.

Note that this object is in an unusable state if the Open
fails to load a document.

=cut
sub Open {
    my $self = shift;
    my ($file) = @_;

    $file = File::Spec->rel2abs($file);
    -f $file or die("Could not open file: File ($file) does not exist\n");
    -r $file or die("Could not open file: Can't read File ($file)\n");

    $self->Close();

    my $oDocument = $self->oWord->Documents->Open({
                FileName => $file,
                ConfirmConversions => $self->rhConst->{False},        #Don't show dialog
                Revert => 1,                                        #Discard changes
                Visible => 0,                                        #No window
                });
    $oDocument->Select;                #Otherwise we have no selection
    $self->oSelection( $self->oWord->Selection ) or die("Could not get Word selection\n");

    $self->oDocument($oDocument);

    $self->Checkpoint();            #Release locks on the template file, otherwise other Word instances may not be able to open it

    return(1);
}





=head2 SaveAs($file, %hOpt)

Save the document to $file (may be a relative file name). %hOpt is:

  format => $format -- Save $file as $format (default:
  Document). Valid values are: Document, DOSText, DOSTextLineBreaks,
  EncodedText, HTML, RTF, Template, Text, TextLineBreaks, UnicodeText

(A common mistake is to inspect the document in another Word instance
when re-running a script. The document will be locked by Word and the
script can't re-create the file.)

=cut
sub SaveAs {
    my $self = shift;
    my ($file, %hOpt) = @_;
    my $format = $hOpt{format} || "Document";

    defined(my $formatConst = $self->rhConst->{"wdFormat$format"}) or croak("Invalid format ($format), use Document, DOSText, DOSTextLineBreaks, EncodedText, HTML, RTF, Template, Text, TextLineBreaks, UnicodeText");

    $file = File::Spec->rel2abs($file);

    eval { $self->oDocument->SaveAs({ FileName => $file, FileFormat => $formatConst }) };
    if($@) {
        my $err = $@;
        if($err =~ /OLE exception from "Microsoft Word":\n\n(.+?)\nWin32::OLE/si) {
            die("Could not save file ($file): $1\n");
        }
        die($err);
    }

    return(1);
}





=head2 Checkpoint()

Checkpoint the document, i.e. save it to a temp file.

This is necessary to do sometimes because Word seems to keep state
until the document is saved, and when using Word automation you tend
to exercise the application in ways they haven't tested properly. And
after a while you get weird errors, just because Word couldn't deal
with all that information.

So you should call this after adding, say, 20K of text to the document
(this is true for Word 2000, it may be better in later versions).

=cut
sub Checkpoint {
    my $self = shift;

    $self->SaveAs( $self->GetFileTemp );

    return(1);
}





=head2 Close()

Discard the current document no-questions-asked (i.e. even if it's not
saved).

Note that this object is in an unusable state until a new document is



( run in 1.543 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )