http://www.perlmonks.org?node_id=259348

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

I've been using win::32 ole to create word documents and have been able to create and manipulate documents successfully. I am now wondering how to add/change form fields in the documents such as a checkbox.

Replies are listed 'Best First'.
Re: create checkbox in word document
by Zero_Flop (Pilgrim) on May 20, 2003 at 03:47 UTC
    As I have mentioned in the past the Macro function is your friend.
    vb code
    Selection.InlineShapes.AddOLEControl ClassType:="Forms.CheckBox.1"

    Just convert this to perl. It may take a little trial and error, but you should be able to do it fairly quickly.
      Zero_Flop is right about the macros. If, however, you can't convert it you can always (as a possible last resort) create the macro in Word and then run it with a Perl command.
      • Create and name the macro in word
      • Declare it at the top of your code
        package MACRO-NAME;
      • When you need the action from the macro, you call it in perl like this:
        $Word->Documents->Open("$dir\\$file") or die "Can't open ", Win32::OLE +->LastError()); $Word->Run("MACRO-NAME");
      This has saved me some time on some hurry-up projects where I didn't have success converting the VBA directly to Perl.
      Selection.InlineShapes.AddOLEControl ClassType:="Forms.CheckBox.1
Re: create checkbox in word document
by cacharbe (Curate) on May 20, 2003 at 13:16 UTC
    To answer your question with perl:
    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 2; my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; my $Doc = $Word->Documents->Add(); $Word->Selection->WholeStory; $Word->Selection->InlineShapes->AddOLEControl({ClassType=>'Forms.Ch +eckBox.1'});

    You'll probably have to read the class documentation to get the names for the ClassTypes for OLEControls, but that's a good exercise for the reader.

    C-.

    ---
    Flex the Geek