[Appendix A] A.18 Chapter 19, OLE Automation
Author: Randal L. Schwartz, Erik Olson & Tom Christiansen
Date: 1999-07-02T01:37:54Z

#

| Appendix A Exercise Answers |

## A.18 Chapter 19, OLE Automation

1. Here are a couple of ways to do it with Internet Explorer 3.x:

   Here's one for the ActiveState distribution (5.003, build 306):

       use OLE;
       $ie = CreateObject OLE "InternetExplorer.Application.1" ||
         die "CreateObject: $!";
       $ie->{Visible} = 1;
       $ie->Navigate("http://www.ora.com/publishing/perl/");

   And here's one for the Perl 5.004 distribution using libwin32:

       use Win32::OLE;
       Win32::OLE::CreateObject("InternetExplorer.Application.1", $ie) || die "CreateObject: $!";
       $ie->{Visible} = 1;
       $ie->Navigate("http://www.ora.com/publishing/perl/");

2. Here are some ways to solve this exercise (this example uses Microsoft Excel 97 - other versions may have slightly different automation objects):

   1. One solution for the ActiveState distribution is:

               use OLE;
               # grab the numbers
               @numbers = <STDIN>;
               # create the automation object
               $xl = CreateObject OLE "Excel.Application" ||
                       die "CreateObject: $!";
               # show it and add a new workbook
               $xl->{Visible} = 1;
               $xl->Workbooks->Add();
               # start at the top left
               $col = "A"; $row = 1;
               foreach $num (@numbers) {
                   chomp($num);
                   $cell = sprintf("%s%d", $col, $row++);
                   # add it to Excel
                   $xl->Range($cell)->{Value}  = $num;
               }

   2. One solution for the Perl 5.004 distribution using libwin32 is:

               use Win32::OLE;
               # grab the numbers
               @numbers = <STDIN>;
               # create the automation object
               Win32::OLE::CreateObject("Excel.Application", $xl) ||
                        die "CreateObject: $!";
               # show it and add a new workbook
               $xl->{Visible} = 1;
               $xl->Workbooks->Add();
               # start at the top left
               $col = "A"; $row = 1;
               foreach $num (@numbers) {
                   chomp($num);
                   $cell = sprintf("%s%d", $col, $row++);
                   # add it to Excel
                   $xl->Range($cell)->{Value}  = $num;
               }

   The first task is to grab our list of numbers (you'll need to enter CTRL-Z to terminate the input). After that, we create an Excel application object, make it visible by setting the `{Visible}` property, and then add a new workbook. Then, we iterate over our array of numbers and add them to Excel, incrementing the row counter as we go. Note that we could have saved this workbook using the Save method, and then terminated Excel using the Quit method, but we chose not to, so that we could see what was going on more easily.

A.17 Chapter 18, CGI Programming |  | B. Libraries and Modules