jjap28 has asked for the wisdom of the Perl Monks concerning the following question:

I was able find/replace text in the body of the Doc file, thanks to internet, but I am not able to find/replace text in the header/footer.



$word = Win32::OLE-> GetActiveObject('Word.Application') || Win32::OLE-> new('Word.Application','Quit'); my $Wordsections = $word->{Sections}; my $sections = $doc->{Sections}; my $n_sections = $sections->{Count}; for my $s ( 1 .. $n_sections ) { my $section = $sections->Item($s); my $headers = $section->{Headers}; my $n_headers = $headers->{Count}; for my $h ( 1 .. $n_headers ) { my $header = $headers->Item($h); print $header->{Range}->{Text}, "\n"; #my $headerRange=$headers->Range("wdHeaderFooterIndex")->InRange +="TRUE"; $Wordsections->Find->ClearFormatting; $Wordsections->Find->{Text} = 'SECRET'; $Wordsections->Find->Replacement->{Text} = "NOFORN"; $Wordsections->Find->Execute({Replace => wdReplaceAll}); } my $footers = $section->{Footers}; my $n_footers = $footers->{Count}; for my $f ( 1 .. $n_footers ) { my $footer = $footers->Item($f); print $footer->{Range}->{Text}, "\n"; } }
  • Comment on find and replace text in header and footers of MS Office doc and excel
  • Download Code

Replies are listed 'Best First'.
Re: find and replace text in header and footers of MS Office doc and excel
by pryrt (Abbot) on Oct 11, 2016 at 14:09 UTC
      I'll give it a shot - thank you

        So these are all Visual basic solutions. I was hoping to see Perl code. I am not familiar with Visual Basic. - Thanks