VBA to Perl macro conversion

Discussion in 'Perl Misc' started by Matt Garrish, Sep 17, 2004.

  1. Matt Garrish

    Matt Garrish Guest

    Not a question, but a solution.

    I had to convert a number of Word files to sgml today and at the same time
    retain the font formatting (in the form of <b>, <i>, etc. tags). I know this
    can be done by saving to html and cleaning up the other html garbage spit
    out, but these files were already nicely styled and the conversion program
    was meant to take advantage of this fact.

    I also couldn't write this search/replace as a VBA macro, because the
    program is automatically launched by a service watching a specific directory
    for files dropped into it (and I couldn't find any way to run a Word macro
    via OLE; I could use $word->Run() from the command line, but it wouldn't
    work when run by the service, even when I changed who the service was run
    as).

    The code snippet below should be self-explanatory, but it sure was a
    headache trying to convert the data structures. Hope this comes in handy for
    anyone trying to do the same. I also encourage anyone else to post to this
    thread any examples of VBA -> Perl code they think others might find useful,
    as I found there is really very little to work from (or feel free to point
    out where my code can be optimized).

    Original VBA macro to find bolded text:


    Dim rngSearch As Word.Range

    Set rngSearch = ActiveDocument.Content

    With rngSearch.Find
    .Format = True
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = False
    .Text = ""
    .Replacement.Text = ""
    .Font.Bold = True

    Do While .Execute

    With rngSearch
    .InsertBefore "<b>"
    .InsertAfter "</b>"
    .Collapse wdCollapseEnd
    End With
    Loop
    End With


    Perl equivalent:

    my $word = Win32::OLE->new('Word.Application', 'Quit');

    my $doc = $word->Documents->Open($infile) or die Win32::OLE->LastError();

    my $range = $doc->Content();

    $range->{Find}->{Format} = 1;
    $range->{Find}->{Forward} = 1;
    $range->{Find}->{Wrap} = wdFindStop;
    $range->{Find}->{MatchWildcards} = 0;
    $range->{Find}->{Text} = '';
    $range->{Find}->{Replacement}->{Text} = '';
    $range->{Find}->{Font}->{Bold} = 1;

    while ( $range->{Find}->Execute() ) {
    $range->InsertBefore('<b>');
    $range->InsertAfter('</b>');
    $range->Collapse(wdCollapseEnd);
    }


    Matt
     
    Matt Garrish, Sep 17, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Domenico Discepola

    win32::ole and excel VBA macro conversion: SmallScroll

    Domenico Discepola, May 5, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    560
    Charlton Wilbur
    May 9, 2004
  2. b wreath
    Replies:
    3
    Views:
    183
    Ben Morrow
    Jun 2, 2004
  3. surf
    Replies:
    0
    Views:
    138
  4. matt
    Replies:
    6
    Views:
    327
  5. Replies:
    10
    Views:
    479
    Tad McClellan
    Oct 6, 2006
Loading...

Share This Page