in reply to Re: Finding a text and replace it with image
in thread Finding a text and replace it with image

site:www.perlmonks.org word replace OLE image
Replace word in Win32::OLE
Win32::OLE Word Search and Replace Script Runs Locally but not as part of CGI
Re: Parsing, searching, replacing content in MS Word documents
  • Comment on Re^2: Finding a text and replace it with image

Replies are listed 'Best First'.
Re^3: Finding a text and replace it with image
by Anonymous Monk on Oct 16, 2008 at 14:59 UTC
    Hi, Below is the tried code.
    #use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft.Word'; # wd constants use Win32::OLE: +:Const 'Microsoft Office'; # mso constants use Win32::OLE::Const 'Microsoft Office'; # -------------------------------------------------- my($input_file) = 'C:\Documents and settings\Jake\Desktop\oledoc.do +c'; $input_file= Win32::GetCwd() . "/$input_file" if $input_file !~ /^(\w: +)?[\/\\]/; die "File $input_file does not exist" unless -f $input_file; my($word)= Win32::OLE->new('Word.Application', 'Quit') || die "Couldn' +t run Word"; my($doc)= $word->Documents->Open($input_file); print "Extract using first method: \n"; #Select the first paragraph my $sel = $word->ActiveDocument->Paragraphs(1)->range->Select; my $myrange = $word->Selection->Range; # Insert the string before the first paragraph $word->Selection->InsertBefore("Beforenewstuff"); print "Range is $myrange\n"; #Then insert the image $myrange->InlineShapes->AddPicture('C:\Documents and Settings\Jake\Des +ktop\Charts1.gif',false,true); $myrange->Collapse ({Direction => wdCollapseEnd}); #Search for 'Addimagehere' in the document $doc->Selection->{Text}="willbereplaced"; $doc->Selection->Collapse(); #Insert the test and gif next to it. This part doesn't work as intende +d $doc->Selection->InlineShapes->AddPicture ('C:\Documents and Settings\ +Jake\Desktop\Charts1.gif', false, true); $word->Selection->Collapse (wdCollapseEnd); # close the document and the application unlink $input_file if -f $input_file; $doc->Close; $word->Quit(); exit(0);
    I am trying to locate 'addimagehere' in the document and to replace it with chart1.gif.
      #Insert the test and gif next to it. This part doesn't work as intended
      How does it work?