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

Hi I am working with Perl Version 5.8.8. I develop programs for a very big company so that I am forced to use this version. I am confronted with a serious problem and I hope that somebody of you can help me. Thanks so far. I am using Tcl-Tk and Word. I try to insert OLE-Objects in Word while Tcl-TK-Widgets are open. But the Program gets stuck.The same effect occurs when I call the Windows-Message-Box ($mw->messageBox) before I try to insert OLE-Objects. I've wrote a very short code-example to demonstrate the effect. The only thing you have to do is to create a test.dat - File in c:\tmp with any content. (Or you modify the code accordingly) When you press the first button, the widget will be closed and all runs well. But when you restart the program and press the second button, the program get stuck. For every hint I would be deeply grateful. Here the code:
#!/usr/bin/perl -w use Tk; use Win32::OLE; my $mw = MainWindow->new; $mw->Button(-text => "Run (Closed Widget)", -command => sub { $mw->destroy() })->pack; $mw->Button(-text => "Run (Opened Widget)", -command => sub { oleTest() })->pack; MainLoop; oleTest(); sub oleTest { my $word = CreateObject Win32::OLE 'Word.Application' or die $!; my $document = $word->Documents->Add(); print "Before OLE\n"; $document->InlineShapes->AddOLEObject("EMBED",'C:\tmp\test.dat'); +# this can be ANY test.dat print "After OLE\n"; $document->SaveAs('c:\tmp\test.docx'); $document->activewindow->{'Visible'}="True"; }

Replies are listed 'Best First'.
Re: TK and Word do not work together
by zentara (Cardinal) on Nov 15, 2011 at 11:03 UTC
    when you restart the program and press the second button, the program get stuck.

    I don't use Windows so I can't run Win32::OLE; but what exactly does it do when it gets stuck? Are there error messages? What exactly happens?

    The Tk code seems to work ok, so the problem seems to be in the sub oleTest{}.

    My advice would be to try and run the oleTest sub as a standalone program, and see what the error is in that code. There is some working OLE code in Win32::OLE: How to get the page number from ms-word?


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Actually you have make that object visible. Please find below the code :

      #!/usr/bin/perl -w use Tk; use Win32::OLE; use Win32::OLE::Const; my $mw = MainWindow->new; $mw->Button(-text => "Run (Closed Widget)", -command => sub { $mw->destroy() })->pack; $mw->Button(-text => "Run (Opened Widget)", -command => sub { oleTest() })->pack; MainLoop; oleTest(); sub oleTest { my $word = CreateObject Win32::OLE 'Word.Application' or die $!; $word->{'Visible'} = 1; my $document = $word->Documents->Add(); print "Before OLE\n"; $document->InlineShapes->AddOLEObject("EMBED",'C:\tmp\test.dat'); +# this can be ANY test.dat print "After OLE\n"; $document->SaveAs('c:\tmp\test.docx'); $document->activewindow->{'Visible'}="True"; }

        Thanks for the code. But even with your modification the program does stuck when I press the second Button
      Thanks for your answer. But if you press the first Button, the oleTest runs well. If th oleTest sub runs as a standaloe program there occurs no error
        But if you press the first Button, the oleTest runs well.

        Sure, the way you have MainLoop; before oleTest(), the first button destroys the Tk event loop, leaving OLE to run unfettered.

        Apparently the Tk eventloop and the way OLE runs, are not compatible, but I can't test that here. Usually, you would put your oleTest() into a separate thread, if it interferes with Tk's eventloop.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh