Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Win32::OLE SaveAs Unicode

by Anonymous Monk
on Jun 19, 2018 at 15:10 UTC ( [id://1216945]=note: print w/replies, xml ) Need Help??


in reply to Win32::OLE SaveAs Unicode

You can do it this way:

#!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw( catfile ); use Cwd qw(cwd); use Win32::OLE; use Win32::OLE::Const 'Microsoft.Word'; # wd constants $Win32::OLE::Warn = 3; my $dir = cwd; my $word = get_word(); $word->{Visible} = 0; my $filename_in = catfile $dir, 'test.docx'; my $doc = $word->{Documents}->Open($filename_in); my $filename_out = catfile $dir, 'mytest.txt'; $doc->SaveAs( { Filename => $filename_out, FileFormat => wdFormatUnicodeText | wdFormatTextLineBreaks, } ); $doc->Close(0); sub get_word { my $word; eval { $word = Win32::OLE->GetActiveObject('Word.Application'); }; die "$@\n" if $@; unless ( defined $word ) { $word = Win32::OLE->new( 'Word.Application', sub { $_[0]->Quit + } ) or die "Oops, cannot start Word: ", Win32::OLE->LastError, "\n"; } return $word; }

Replies are listed 'Best First'.
Re^2: Win32::OLE SaveAs Unicode
by Anonymous Monk on Jun 19, 2018 at 15:42 UTC

    Sorry, I was too fast, changing to

    $doc->SaveAs( { Filename => $filename_out, FileFormat => wdFormatText, Encoding => '1200', } );

    does it better.

    A note: I always use Word Macro Recorder and do my processing manually,

    then I 'translate' the VBA code to perl code,

    which is straight forward normally.

      Strange enough, if I save to a temporary file with

      my $filename_out = new File::Temp( UNLINK => 0, SUFFIX => '.txt' );

      I get the following error: Win32:OLE UsedRange error 0x80020005: "Type mismatch"

        File::Temp->new
        returns a file handle, not a file name.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      It works (with the correction), thank you! I'll have a look at the idea of VBA!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found