【问题标题】:How can I change the colourindex of a word in a string for msword using Win32:OLE?如何使用 Win32:OLE 更改 msword 字符串中单词的颜色索引?
【发布时间】:2010-03-17 03:54:06
【问题描述】:

我实际上是在尝试更改数组中带有大括号的第一个单词的颜色索引,以便它们在 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 和 Windows XP。这个数组只是一个例子,数组的内容会改变,但第一个带大括号的单词必须用红色打印。

【问题讨论】:

  • “感觉我们很棒”听起来很性感,否则毫无意义。即使那样,这也是一个延伸。我知道你的母语不是英语,所以不要这么说,除非你想让人们嘲笑你。 “(perl) 让我们感觉很棒”、“(perl) 让我感觉很棒”或“(perl) 感觉很棒”是可以接受的。
  • 你知道,思南,只要你继续用出色的答案奖励他,他就不会改变他的行为。 :)
  • @brian 真的,非常正确。但是,请注意,我的代码也故意将(language) 涂成红色。另外,我尝试遵循网站是关于问题和答案的原则,而不是个人海报。所以,我实际上选择等待大约 10 个小时,然后才回答他的紧急问题。
  • 嘿伙计们告诉我哪里出错了?我已经接受了所有与我的问题相关的答案。当然,我不是只寻找答案的人对不起,如果我的事情伤害了你们所有人。我认为这样还不够好,无法按照你们的期望在这里发布问题。对不起,伙计们

标签: perl ms-word ole


【解决方案1】:
#!/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.

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多