Encoding problem with automation of Word by Perl

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

In the code snippet below @foutput is an array of 'paragraphs': which are
arrays of 'text items': which are arrays with two elements, the first being
a text string and the second another text string which holds formatting
information (currently 'b' for bold 's' for superscript and '' for normal).

The code works in than it opens a Word document and produces formatted text
therein. The problem is that non-ascii Unicode characters do not transfer
cleanly. I expect that Perl and Word are making different assumptions about
what encoding is in use (Word seems to be recieving utf-8 but interpreting
it as 'code page 1252') but I don't know how change it.

Here is the code snippet (use strict and use warnings are in operation at
the top of the file) :

elsif ($opt) {
  use Win32::OLE;
  my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
  $word-> = 1;
  my $document = $word->Documents->Add;
  my $selection = $word->Selection;
  my $i = 0;
  foreach my $para (@foutput) {
   $i++; last if $i == 5; # just a few for debugging
   foreach (@) {
     if (@[1] eq "") {
     elsif (@[1] eq "b") {
      $selection->Font-> = 1;
      $selection->Font-> = 0;
     elsif (@[1] eq "s") {
      $selection->Font-> = 1;
      $selection->Font-> = 0;
     else {
      die "Unknown formatting: " . @[1];
    $selection -> TypeParagraph;

Re: Encoding problem with automation of Word by Perl

Quoted text here. Click to load it

Sorry, posted too soon as I found the answer shortly afterwards in the Docs
to Activestate perl. Adding:

Win32::OLE->Option(CP => Win32::OLE::CP_UTF8());below the line use
Win32::OLE;solves the problem.Dave

Site Timeline