如何使用 Win32:OLE 更改 msword 字符串中单词的颜色索引?

发布于 2024-08-25 14:27:52 字数 561 浏览 6 评论 0原文

我实际上正在尝试更改数组中第一个带有大括号的单词的颜色索引,以便它们在 Word 2003 中以正确的颜色显示。

例如,如果我有一个这样的数组:

@array="
        (This) is (perl),
         perl is a great (language),
         we can do anything with perl,
         (perl) feels us great."

我需要第一个单词的颜色位于括号()内,即(This)(perl),其中()为红色其余内容为黑色。并在 MS Word 2003 中打印数组的全部内容:

我正在使用 Win32::OLE< /a> 和 Windows XP。这个数组只是一个例子,数组的内容会改变,但第一个带大括号的单词必须以红色打印。

I am actually trying to change the color index for the first word with braces in an array so they show up in the right color in Word 2003.

For example, if I have an array like this:

@array="
        (This) is (perl),
         perl is a great (language),
         we can do anything with perl,
         (perl) feels us great."

I need the colour of the first word that is inside the parentheses (), i.e. (This) and (perl) including () to be in red and rest of the contents in black. and print the entire contents of the array in MS Word 2003:

I'm using Win32::OLE and Windows XP. This array is just an example the contents of an array will change but first word with braces has to be printed in red.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2024-09-01 14:27:53
#!/usr/bin/perl

use strict; use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;

my $word = get_app('Word.Application');
$word->{Visible} = 1;

my $doc = $word->Documents->Add;

while ( my $line = <DATA> ) {
    my @chunks = split /(\(\w+\))/, $line;
    my $seen;
    for my $chunk ( @chunks ) {
        my $sel = $word->Selection;
        my $font = $sel->Font;
        if ( $chunk =~ /^\(/ and not $seen) {
            $font->{ColorIndex} = wdRed;
            $seen = 1;
        }
        else {
            $font->{ColorIndex} = wdBlack;
        }
        $sel->TypeText($chunk);
    }
}

sub get_app {
    my ($class) = @_;
    my $app;
    eval {
        $app = Win32::OLE->GetActiveObject($class);
    };

    if ( my $ex = $@ ) {
        die $ex, "\n";
    }

    unless(defined $app) {
        $app = Win32::OLE->new($class, sub { $_[0]->Quit })
            or die "Oops, cannot start '$class': ",
                   Win32::OLE->LastError, "\n";
    }
    return $app;
}

__DATA__
(This) is (perl),
perl is a great (language),
we can do anything with perl,
(perl) feels us great.
#!/usr/bin/perl

use strict; use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;

my $word = get_app('Word.Application');
$word->{Visible} = 1;

my $doc = $word->Documents->Add;

while ( my $line = <DATA> ) {
    my @chunks = split /(\(\w+\))/, $line;
    my $seen;
    for my $chunk ( @chunks ) {
        my $sel = $word->Selection;
        my $font = $sel->Font;
        if ( $chunk =~ /^\(/ and not $seen) {
            $font->{ColorIndex} = wdRed;
            $seen = 1;
        }
        else {
            $font->{ColorIndex} = wdBlack;
        }
        $sel->TypeText($chunk);
    }
}

sub get_app {
    my ($class) = @_;
    my $app;
    eval {
        $app = Win32::OLE->GetActiveObject($class);
    };

    if ( my $ex = $@ ) {
        die $ex, "\n";
    }

    unless(defined $app) {
        $app = Win32::OLE->new($class, sub { $_[0]->Quit })
            or die "Oops, cannot start '$class': ",
                   Win32::OLE->LastError, "\n";
    }
    return $app;
}

__DATA__
(This) is (perl),
perl is a great (language),
we can do anything with perl,
(perl) feels us great.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文