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.

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:

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.

-- Ken


In reply to Re: Add hyperlink to paticular lines of MS doc by kcott
in thread Add hyperlink to paticular lines of MS doc by Akku

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.