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

Hello wise ones.
I have a script that creates a multi page word document containing network adapter info (It will do something else when done). I am able to create the header and footer for the current page but need to have the header and footer for page 1 be different than all the others. If someone could take a look at the below code and give me a shove It would be greatly appreciated. I am of course open to any criticism as to my methods as well.

Thanks in advance!!!

use Win32::OLE; # Object Linking and Embeddi +ng use Win32::OLE::Const 'Microsoft Word'; # Defines constants word kno +ws use Win32::Process ; $picturefile = "c:\\temp\\picture.gif"; $objWord = Win32::OLE->new('Word.Application') or die "Cannot start Word" ; $objWord->{Visible} = 0 ; # 0 = Don't watch what happens 1 = wa +tch what happens $objWord->{DisplayAlerts} = 0 ; # 0 = do not prompt 1 = prompt $objWord->{Caption} = 'TestDoc DOC'; $objDoc = $objWord->Documents->Add(); $objSelection = $objWord->Selection; #################################### #### Set up document formatting #### #################################### $objSelection->PageSetup->{LineNumbering} = 0 ; $objSelection->PageSetup->{Orientation} = wdOrientPortrait ; $objSelection->PageSetup->{TopMargin} = 18 ; # .25 inch $objSelection->PageSetup->{BottomMargin} = 18 ; # .25 inch $objSelection->PageSetup->{LeftMargin} = 22 ; # .25 inch $objSelection->PageSetup->{RightMargin} = 18 ; # .25 inch $objSelection->PageSetup->{Gutter} = 0 ; # 0. inch $objSelection->PageSetup->{HeaderDistance} = 18 ; # .25 inch $objSelection->PageSetup->{FooterDistance} = 18 ; # .25 inch $objSelection->PageSetup->{PageWidth} = 612 ; # 8.5 inches $objSelection->PageSetup->{PageHeight} = 792 ; # 11.0 inches $objSelection->PageSetup->{FirstPageTray} = wdPrinterDefaultBin ; $objSelection->PageSetup->{OtherPagesTray} = wdPrinterDefaultBin ; $objSelection->PageSetup->{SectionStart} = wdSectionNewPage ; $objSelection->PageSetup->{OddAndEvenPagesHeaderFooter} = 0 ; $objSelection->PageSetup->{DifferentFirstPageHeaderFooter} = 1 ; $objSelection->PageSetup->{VerticalAlignment} = wdAlignVerticalTop ; $objSelection->PageSetup->{SuppressEndnotes} = 0 ; $objSelection->PageSetup->{MirrorMargins} = 0 ; ############################### #### Start Creating Header #### ############################### $objWord->ActiveWindow->ActivePane->View->{Type} = wdPageView ; $objWord->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurrentPa +geHeader ; # $objSelection->ParagraphFormat->Borders(wdBorderLeft)->{LineStyle} = w +dLineStyleNone ; $objSelection->ParagraphFormat->Borders(wdBorderRight)->{LineStyle} = +wdLineStyleNone ; $objSelection->ParagraphFormat->Borders(wdBorderTop)->{LineStyle} = wd +LineStyleNone ; $objSelection->ParagraphFormat->Borders(wdBorderBottom)->{LineWidth} = + wdLineWidth150pt ; $objSelection->ParagraphFormat->Borders(wdBorderBottom)->{LineStyle} = + wdLineStyleTriple ; $objSelection->ParagraphFormat->Borders->{DistanceFromTop} = 1 ; $objSelection->ParagraphFormat->Borders->{DistanceFromLeft} = 4 ; $objSelection->ParagraphFormat->Borders->{DistanceFromBottom} = 1 ; $objSelection->ParagraphFormat->Borders->{DistanceFromRight} = 4 ; $objSelection->Style('ActiveDocument->Styles' =>"Header") ; $objSelection->Font->{Name} = 'Times New Roman' ; $objSelection->Font->{Size} = 8 ; $objSelection->Font->{Italic} = 1 ; $objSelection->Font->{ColorIndex} = wdBlack ; # INSERT PICTURE #### $objSelection->InlineShapes->AddPicture($picturefile); $headertext = "\n\nTHIS IS THE HEADER FOR ALL PAGES BUT PAGE ONE\n"; $objSelection->TypeText("$headertext") ; ############################## #### Done Creating Header #### ############################## ############################### #### Start Creating Footer #### ############################### $objWord->ActiveWindow->ActivePane->View->{SeekView} = wdSeekCurrentPa +geFooter ; # $objSelection->ParagraphFormat->Borders(wdBorderLeft)->{LineStyle} = w +dLineStyleNone ; $objSelection->ParagraphFormat->Borders(wdBorderRight)->{LineStyle} = +wdLineStyleNone ; $objSelection->ParagraphFormat->Borders(wdBorderTop)->{LineWidth} = wd +LineWidth225pt ; $objSelection->ParagraphFormat->Borders(wdBorderTop)->{LineStyle} = wd +LineStyleSingle ; $objSelection->ParagraphFormat->Borders(wdBorderBottom)->{LineStyle} = + wdLineStyleNone ; $objSelection->ParagraphFormat->Borders->{DistanceFromTop} = 1 ; $objSelection->ParagraphFormat->Borders->{DistanceFromLeft} = 4 ; $objSelection->ParagraphFormat->Borders->{DistanceFromBottom} = 1.25 ; $objSelection->ParagraphFormat->Borders->{DistanceFromRight} = 4 ; $objSelection->Style('ActiveDocument->Styles' =>"Footer") ; $objSelection->Font->{Name} = 'Times New Roman' ; $objSelection->Font->{Size} = 8 ; $objSelection->Font->{Italic} = 1 ; $objSelection->Font->{ColorIndex} = wdBlack ; $footertext = "\nTHIS IS THE FOOTER TEXT FOR ALL PAGES BUT PAGE ONE\n" +; $objSelection->TypeText("$footertext") ; $objSelection->TypeText("\t") ; $objWord->NormalTemplate->AutoTextEntries("Page X of Y")->Insert($objS +election->Range); ############################## #### Done Creating Footer #### ############################## ####################################### #### START ADDING TEXT TO THE BODY #### ####################################### $objWord->ActiveWindow->ActivePane->View->{SeekView} = wdSeekMainDocum +ent ; $objSelection->Font->{Name} = 'Arial'; $objSelection->Font->{Size} = '18'; $objSelection->TypeText('Network Adapter Report'); $objSelection->TypeParagraph(); $objSelection->Font->{Size} = '20'; $objSelection->TypeText('' . VBS::Date()); $objSelection->TypeParagraph(); $objSelection->TypeParagraph(); $objSelection->Font->{Size} = '10'; ####################################### #### Done ADDING TEXT TO THE BODY #### ####################################### ######################################################### ### Adding network adapter configuration to document #### ######################################################### $strComputer = '.'; $objWMIService = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer +. '\\root\\cimv2'); $colItems = $objWMIService->ExecQuery('Select * from Win32_NetworkAdap +terConfiguration'); foreach my $objItem (in $colItems) { $objSelection->Font->{Bold} = 1; $objSelection->TypeText('ARP Always Source Route: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->ArpAlwaysSourceRoute); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('ARP Use EtherSNAP: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->ArpUseEtherSNAP); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Caption: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->Caption); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Database Path: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->DatabasePath); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Dead GW Detection Enabled: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->DeadGWDetectEnabled); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Default IP Gateway: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->DefaultIPGateway); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Default TOS: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->DefaultTOS); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Default TTL: '); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->DefaultTTL); $objSelection->TypeParagraph(); $objSelection->Font->{Bold} = 1; $objSelection->TypeText('Description: '); $objSelection->Font->{Bold} = 1; $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $objItem->Description); $objSelection->TypeParagraph(); $objSelection->TypeParagraph(); } ########################################################## #### Done network adapter configuration to document ###### ########################################################## $objDoc->SaveAs('C:\\temp\\testdoc.doc'); $objWord->Quit(); package VBS; use Win32::OLE::NLS qw(:TIME :DATE GetLocaleInfo GetUserDefaultLCID LOCALE_SMONTHNAME1 LOCALE_SABBREVMONTHNAME1 LOCALE_SDAYNAME1 LOCALE_SABBREVDAYNAME1 LOCALE_IFIRSTDAYOFWEEK LOCALE_SDATE LOCALE_IDATE LOCALE_SGROUPING ); use Win32::OLE::Variant; use constant EPOCH => 25569; use constant SEC_PER_DAY => 86400; our $lcid; BEGIN { $lcid = GetUserDefaultLCID(); Win32::OLE->Option(LCID => $lcid); } # Return the local time in seconds since the epoch. sub _localtime_in_sec { require Time::Local; return 2.0 * CORE::time() - Time::Local::timelocal(gmtime); } # Extract specific information out of a date. sub _extract_from_date { my($date,$method,$format) = @_; return unless $date; unless (UNIVERSAL::isa($date, "Win32::OLE::Variant")) { $date = Variant(VT_DATE, $date); } return $date->$method($format, $lcid); } # Returns the current system date. sub Date { return Variant(VT_DATE, EPOCH + int(_localtime_in_sec()/SEC_PER_DA +Y)); }

Formatting and readmore tags added by davido per consideration.

Replies are listed 'Best First'.
Re: Win32::OLE first page header/footer how?
by NetWallah (Canon) on Jan 25, 2005 at 21:08 UTC
    A couple of suggestions:
    • Use a Word Template (.dot) file that contains the formatting info you need. That is easier to modify than code.
    • Use perl sub to manage repetitive code
      sub FormatThis{ my %h = @_; foreach (keys %h){ $objSelection->Font->{Bold} = 1; $objSelection->TypeText($_); $objSelection->Font->{Bold} = 0; $objSelection->TypeText('' . $h{$_}); $objSelection->TypeParagraph(); } } ########################## # Now you can call it like this ... FormatThis( 'ARP Always Source Route: ' => $objItem->ArpAlwaysSource +Route, 'ARP Use EtherSNAP: ' => $objItem->ArpUseEtherSNAP, ); # Etc... }
      This would avoid the apparent typo you have at "Description".

        ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

      Thanks for the suggestions. I am in the right direction and moving forward now.
Re: Win32::OLE first page header/footer how?
by wfsp (Abbot) on Jan 25, 2005 at 18:47 UTC
    In word2003 there is a
    PageSetup.DifferentFirstPageHeaderFooter
    property. The vba help file only shows how to test if this propery is true.

    An article here: http://support.microsoft.com/kb/q269565/
    talks about: "The second section contains three pages. In this section, the first page header and footer are different from the other two pages."

    Hope this helps.

Re: Win32::OLE first page header/footer how?
by holli (Abbot) on Jan 25, 2005 at 19:54 UTC
    In such a case, i find it simple to use the macro-recorder. Do what you need manually, strip the relevant parts from the resulting macro-code and translate that to perl.

    holli, regexed monk