By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

help with word object model and perl

P: 1
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::Variant;
use Win32::OLE::Const;
use Win32::Process ;
use Win32::OLE::Const 'Microsoft Word';

my $VERSION=$ENV{CLEARCASE_ID_STR};
$VERSION=~s/^\\//;
my $cidate=`date +%d-%B-%Y`;
chomp($cidate);
my $author=$ENV{CLEARCASE_USER};
my $comment=$ENV{CLEARCASE_COMMENT};

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

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

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

my $wd = Win32::OLE::Const->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->BuiltInDocumentProperties("Subject")->{Value};
#print "Subject = $prevsub\n";
$doc->BuiltInDocumentProperties("Subject")->{Value} = "$VERSION";
$word->Options->{SavePropertiesPrompt} = $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_Version = 11;
if ($WordVersion < $Word_2003_Version) {
$word->ActiveWindow->ActivePane->View->{Type} = wdPageView ;
$word->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurrentPageHeader;
my $Active = $word->Selection() ;

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

$find->ClearFormatting;
$find->Replacement->ClearFormatting;

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

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

$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->NextStoryrange != "") {
$obj_Story = $obj_Story->NextStoryRange;
$obj_Story->Fields->{Update};
}
}
}

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

$find->ClearFormatting;
$find->{Text} = "REVISION HISTORY^p";
$find->Forward => $wd->{True};
$find->Wrap => $wd->{wdFindContinue};
$result=$find->Execute;

$word->ActiveWindow->ActivePane->View->{SeekView}=wdGoToTable;
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=$Activetbl->Cell($row, $col)->Range;
$cell->Text = "Name";
}


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

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


# close word

$word->Quit();
Oct 5 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.