G'day Akku,
I'm not in a position to test this: no Perl on MSWin; no MS Word; no Win32::OLE.
However, I see some anomalies in your code which might be the cause of the problem.
-
You declare $end but then use $end1 elsewhere in your code.
-
You set $start to zero and never change it.
Without changing any other parts of your code, would your for loop code be better as something closer to this:
my ($c, $start, $end) = (2, 0, 0);
foreach $elemetns(keys %hash)
{
$start = $end;
$end += length $elemetns;
...
my $r = $document->Range($start, $end);
...
}
[Note: The += is a guess; it depends on how Range() works; just = might be correct.]
Unrelated to your current issue but possibly a source of confusion or errors later on:
-
I see no reason for %hash. You only use its keys in the code shown and the name is meaningless.
I'd suggest my @elements = (...); would be a better choice here.
-
$elemetns is probably a poor choice. It suggests plural although it's a scalar value only ever holding a single string. It also looks like a misspelling of element which someone (possibly you) may spell correctly later in your script development. I think $element would be a better choice.
So, I'd suggest these changes:
...
my @elements = (
'This is a test line',
'This is second test Line',
'This is the third line',
);
...
my ($c, $start, $end) = (2, 0, 0);
for my $element (@elements)
{
$start = $end;
$end += length $element;
...
my $r = $document->Range($start, $end);
...
}
$c and $r are also problematical.
They're basically meaningless names which conveyed nothing when I first encountered them.
Perhaps $colour_index and $range would be better names.
Also, you assign $c = 2. What does '2' mean? While you may know today, will you when you revisit this code at some later time. Will the next maintainer know?
Either use a meaningfully named constant or add a comment explaining what '2' refers to.
As a general rule, arbitrary constant numbers popping in pieces of code are difficult to understand (without doing extra research which shouldn't be necessary) and are error-prone.
|