Encoding problem with automation of Word by Perl

Discussion in 'Perl Misc' started by Dave, Jun 1, 2005.

  1. Dave

    Dave Guest

    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{w}) {
    use Win32::OLE;
    my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
    $word->{'Visible'} = 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 (@{$para}) {
    if (@{$_}[1] eq "") {
    $selection->TypeText(@{$_}[0]);
    }
    elsif (@{$_}[1] eq "b") {
    $selection->Font->{Bold} = 1;
    $selection->TypeText(@{$_}[0]);
    $selection->Font->{Bold} = 0;
    }
    elsif (@{$_}[1] eq "s") {
    $selection->Font->{Superscript} = 1;
    $selection->TypeText(@{$_}[0]);
    $selection->Font->{Superscript} = 0;
    }
    else {
    die "Unknown formatting: " . @{$_}[1];
    }
    }
    $selection -> TypeParagraph;
    }
    Dave, Jun 1, 2005
    #1
    1. Advertising

  2. Dave

    Dave Guest

    "Dave" <> wrote in message
    news:qZkne.5780$%...
    > 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{w}) {
    > use Win32::OLE;
    > my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
    > $word->{'Visible'} = 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 (@{$para}) {
    > if (@{$_}[1] eq "") {
    > $selection->TypeText(@{$_}[0]);
    > }
    > elsif (@{$_}[1] eq "b") {
    > $selection->Font->{Bold} = 1;
    > $selection->TypeText(@{$_}[0]);
    > $selection->Font->{Bold} = 0;
    > }
    > elsif (@{$_}[1] eq "s") {
    > $selection->Font->{Superscript} = 1;
    > $selection->TypeText(@{$_}[0]);
    > $selection->Font->{Superscript} = 0;
    > }
    > else {
    > die "Unknown formatting: " . @{$_}[1];
    > }
    > }
    > $selection -> TypeParagraph;
    > }
    >


    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
    Dave, Jun 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Howard
    Replies:
    1
    Views:
    3,268
    Matt Garrish
    Jul 2, 2004
  2. Ben Fidge
    Replies:
    0
    Views:
    496
    Ben Fidge
    Jul 16, 2003
  3. Rachel

    word automation in asp.net

    Rachel, Aug 1, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Chris Jackson
    Aug 4, 2003
  4. apondu
    Replies:
    0
    Views:
    581
    apondu
    Jul 19, 2007
  5. Michael G. Schneider

    Modifying a Word document without using Word Automation

    Michael G. Schneider, Dec 15, 2003, in forum: ASP General
    Replies:
    5
    Views:
    271
    el.c. - myLittleTools.net
    Dec 16, 2003
Loading...

Share This Page