help with word object model and perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • funeeldy
    New Member
    • Oct 2006
    • 1

    help with word object model and perl

    I need to locate a particular table in a document. I cannot hardcode the table number since it could be different in every doc.
    I do have some header text that comes right before it consistently, though. So, I tried to find that text, and then move to the next table.
    Then I need to add a row to the table which already has 4 columns. I need to maintain the old content of the table too.
    I think I am pretty close, but having a little trouble converting the vb into perl. Do you mind looking this script over and telling me where I went wrong, PLEASE?
    use Win32::OLE qw(in);
    use Win32::OLE;
    use Win32::OLE::Var iant;
    use Win32::OLE::Con st;
    use Win32::Process ;
    use Win32::OLE::Con st 'Microsoft Word';

    my $VERSION=$ENV{C LEARCASE_ID_STR };
    $VERSION=~s/^\\//;
    my $cidate=`date +%d-%B-%Y`;
    chomp($cidate);
    my $author=$ENV{CL EARCASE_USER};
    my $comment=$ENV{C LEARCASE_COMMEN T};

    my $file=$ENV{CLEA RCASE_PN};
    $file=~s/\\/\//g;

    # start up ms word
    #print "starting word\n";
    #print `date`;

    my $word = Win32::OLE->new('Word.Appl ication','Quit' ) or die "Could not load MS Word";

    my $wd = Win32::OLE::Con st->Load($word);
    #print `date`;

    $word->{'Visible'} = 1; # if you want to see what's going on

    # open the word document, you need the full path name

    my $doc = $word->Documents->Open( { FileName => $file } );
    #print "opened the doc\n";

    die "Failed to load doc $file\n" unless $doc;

    #print `date`;
    $txt = $word->version;
    $WordVersion = substr($txt, 0, index($txt,".") );
    #$WordVersion = 10;
    #print "figured out the version\n";
    #print `date`;

    $prevsub = $doc->BuiltInDocumen tProperties("Su bject")->{Value};
    #print "Subject = $prevsub\n";
    $doc->BuiltInDocumen tProperties("Su bject")->{Value} = "$VERSION";
    $word->Options->{SavePropertie sPrompt} = $FALSE;

    # If Word version before 2003, then set the fields separately from the property
    # The fields cannot be updated in versions older than 2003.
    #
    $Word_2003_Vers ion = 11;
    if ($WordVersion < $Word_2003_Vers ion) {
    $word->ActiveWindow->ActivePane->View->{Type} = wdPageView ;
    $word->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurrentPa geHeader;
    my $Active = $word->Selection() ;

    # do a search and replace
    my $find=$Active->Find;

    $find->ClearFormattin g;
    $find->Replacement->ClearFormattin g;

    $find->{Text} = "$prevsub";

    # print "version is $VERSION\n";
    $find->Replacement->{Text} = "$VERSION";
    $find->Forward => $wd->{True};
    $find->Wrap => $wd->{wdFindContinu e};

    $find->Execute( {
    Replace => $wd->{wdReplaceAll} ,
    } );

    } else {
    # Modify all Fields in the document to reflect the current value
    # of their associated Document Variables. The following is specific
    # to Microsoft Word 2003 and later
    foreach my $obj_Story (in($doc->StoryRanges) ) {
    $obj_Story->Fields->{Update};
    while ($obj_Story->NextStoryran ge != "") {
    $obj_Story = $obj_Story->NextStoryRange ;
    $obj_Story->Fields->{Update};
    }
    }
    }

    $word->ActiveWindow->ActivePane->View->{Type} = wdPageView ;
    $word->ActiveWindow->ActivePane->View->{SeekView} = wdSeekMainDocum ent;
    my $Active = $word->Selection() ;
    my $find=$Active->Find;

    $find->ClearFormattin g;
    $find->{Text} = "REVISION HISTORY^p";
    $find->Forward => $wd->{True};
    $find->Wrap => $wd->{wdFindContinu e};
    $result=$find->Execute;

    $word->ActiveWindow->ActivePane->View->{SeekView}=wdG oToTable;
    my $Activetbl = $word->Selection() ;
    print "active tbl is $Activetbl\n";
    my $row=$Activetbl->Rows->Add();
    print "row is $row\n";
    for ($col = 4; $col>=0&$col<=4 ; $col++) {
    my $cell=$Activetb l->Cell($row, $col)->Range;
    $cell->Text = "Name";
    }


    # save the file
    $doc->SaveAs( "$file" );

    $doc->Close( { SaveChanges => $wd->{wdSaveChanges } } );
    #$doc->Close( { SaveChanges => $wd->{wdDoNotSaveCh anges} } );


    # close word

    $word->Quit();
Working...