Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Win32::OLE not opening a window for me

by Anonymous Monk
on Mar 23, 2012 at 12:52 UTC ( #961204=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


I took a code fragment to experiment with opening a Word File from Perl.

The below, snagged from the web, returns the message, "Main::$doc only used once" which may be something.

WINWORD.EXE is being started as I can see it in the task manager.

Any tips much appreciated - do I need to do something else to actually view the instance. I believe I read that visible is the default.


my $word = Win32::OLE->new( 'Word.Application' ); $doc = $word->Documents->Open( { FileName => $document_path, , ReadOnly => 1 } );

Replies are listed 'Best First'.
Re: Win32::OLE not opening a window for me
by davies (Parson) on Mar 23, 2012 at 13:17 UTC
    use strict; use warnings; use Win32::OLE; my $word = Win32::OLE->new( 'Word.Application' ); $word->{Visible} = 1;

    You need to make it visible. You may need to check the task manager for orphaned instances of Word (or reboot your machine).


    John Davies

      Ah, that's it. More confident it's working now I can actually see it!



Re: Win32::OLE not opening a window for me
by ww (Archbishop) on Mar 23, 2012 at 13:27 UTC
    "the message, "Main::$doc only used once" which may be something."

    Yes, it indeed is "something."

    It is telling you that you have used the variable $doc only once (in scope) in your script.

    Perhaps you've drawn an incorrect conclusion about visibility (which I infer from your title): $doc is visible to perl in (some part of) the script; if you want "eyeballs on" the contents of $doc, you'll need to send it to your console (screen), and, if you want an accurate display, decode from Word's character set to the once used by your console. (see davies's Re: Win32::OLE not opening a window for me above)....

    ( ... and, of course, that presumes that you've provided an accurate path and filename in $document_path. More detail on the script may be needed to ascertain that, if there's another problem.)

    Update: ... 'oops!', says /me. I now think I misunderstood/misread part of your problem. The message you quote is, indeed, telling you what I said in the first two para after the quote, but John Davies' understanding of what you were talking about re "visible" seems likely to be far closer to be an answer to your visibility issue than that in the portion marked by strikethrus above.

    Update 2: PS: if you're using strict (highly recommended because it can help find one's errors), then you should have received another message... something to the general effect that " ... $doc requires requires explicit package name at line nnn." The more we know about things like error messages and warnings, the better the help we provide (as long as we don't misread the problem :-( ).

      Thanks for taking the time - yes, I did want to get it to actually open as a Word document, though for my own satisfaction before trying to do something useful with it so will be aware of converting the character set which will still apply, I think. But the other reply did work for viewing.

      The console printed a message from Word telling me when the document didn't exist which was useful as I hadn't put in any error checking.



        In general, error checking on open -- open ($FH "<", $filename) or die "Can't open $filename, $!";Note1 for one possible example -- and using strict and warnings are almost certain to be very helpful when you're experimenting in the way you describe..

        I don't use Win32::OLE in the manner you are (and in fact, rarely in any fashion), so I have to ask: Are you sure the message to the console came from Word? I suspect it more likely came from Win32::OLE (or, less likely) from Perl itself).

        Update: Note1 In a msg, davies points out (++) that the open illustrated in para 1 is a generalized form may be confusing in context. I agree, and should have phrased it to make clear that it is probably not applicable to file opens performed by/with Win32::OLE or the like.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961204]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-05-25 17:43 GMT
Find Nodes?
    Voting Booth?