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

Dear PerlMonkers, I'm still having difficulty with a script to update a Word document. I have a Perl script that basically does what I want it to do which is to seek the current page header, select all, then do an update, which updates the display in the header of the document to display the value of a DocProperty field. That part of the code looks like this:

$word_app->ActiveWindow->ActivePane->View->{Type} = wdPrintView; $word_app->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurrentP +ageHeader; $word_app->Selection->WholeStory; $word_app->Selection->Field->Update;

Now, the problem is that many Word Documents have more than one Section, so I’m now trying to loop this to go through and update every section. The above code only updates the first section of the document so… now I’m doing:

My $sections = $doc->Sections->Count(); $word_app->ActiveWindow->ActivePane->View->{Type} = wdPrintView; For ($i=1; $i<=$sections; $i++){ #Macro to translate: Selection.GoTo What:=wdGoToSection, which:=wd +GoToNext, Count:=1, Name:=”” #Next line is an attempt to translate but it is NOT working, saying + bad parameter… $word_app->ActiveWindow->Selection->GoTo(“wdGoToSection”, “wdGoToNe +xt”, 1, 0); $word_app->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurre +ntPageHeader; $word_app->Selection->WholeStory; $word_app->Selection->Field->Update; }

This second part still only updates the first section because the one line to goto the next section is NOT working... Do you have any ideas that would help me out?? Thanks so much! Mark

Replies are listed 'Best First'.
Re: Macro goto xlate help
by Corion (Patriarch) on Oct 11, 2010 at 17:24 UTC

    See Win32::OLE on how to pass named parameters, and Win32::OLE::Constant on how to get at the needed constants. Basically, the following should work (and Perl does not use fancy quotes):

    $word_app->ActiveWindow->Selection->GoTo({ What => wdGoToSection, whic +h => wdGoToNext, Count => 1, Name => ''\);

      Thanks Corion! That worked!

Re: Macro goto xlate help
by Anonymous Monk on Oct 11, 2010 at 19:30 UTC

    1. You're never actually using the loop's $i variable in the code inside the loop.

    2. The second snippet has syntax errors ("For" and curly quotes). You've apparently typed this in or copied it to and then copied it out of Word, which will mangle plain text in this way. You should show us the actual code you're using.

    3. The Perlish way to loop over a value between 1 and $sections is with something like for my $i (1..$sections) {}. You might also want to consider a more descriptive variable name than "$i".