I am developing an application that replaces character strings in a Word document.
The code below does this as I need. The replacement text is set up in the lines $doll_str{xx<n>} = replacement string.
However, I would like the replacement string to include none normal alpha/numeric characters such as
those found in the Windings style and in particular the ‘tick’.
More in hope than expectation I have tried copying the tick mark from a Word document into Perl but this gave just a double quote.
How can this be done?
use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; # set $filepath to the full path to the directory where the file to be + changed, test.doc, is stored my $filepath = 'C:/tmp'; my $oldfile = $filepath . '/test.doc'; my $newfile = $filepath . '/test1.doc'; my ($oldtext, $newtext); my (%doll_str, $doll_item, $search_res, $replace_res, $exec_res); $doll_str{xx1} = 'fred 1'; $doll_str{xx2} = 'orange 2'; $doll_str{xx3} = 'green 3'; $doll_str{xx4} = 'blue 4'; $doll_str{xx5} = 'black 5'; my $word = Win32::OLE-> GetActiveObject('Word.Application') || Win32::OLE-> new('Word.Application','Quit'); my $doc = $word-> Documents->Open("$oldfile"); # is application visible 0=no 1=yes $word-> {visible} = 0; my $search = $doc-> Content->Find; my $replace = $search-> Replacement; $search-> {Text} = $oldtext; $replace-> {Text} = $newtext; $search-> Execute({Replace => wdReplaceAll}); foreach $doll_item (sort {$a cmp $b} keys %doll_str) { $search-> {Text} = $doll_item; $replace-> {Text} = $doll_str{$doll_item}; $exec_res = $search-> Execute({Replace => wdReplaceAll}); print "item <$doll_item> exec <$exec_res>/n"; } # save word file $word-> ActiveDocument->SaveAs($newfile); # close word file $doc-> Close(); $word-> Quit();

In reply to Replacing none alpha/numeric characters in a Word document by merrymonk

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.