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

Hi All,
I need to open text file with microsoft word like openwith option in explorer. How can i do that? Currently i am using the below command to open my file.

system("$wordoutput");

Replies are listed 'Best First'.
Re: system command
by cdarke (Prior) on Mar 31, 2010 at 09:46 UTC

      Does that work if the OPs text file doesn't have an extension that is associated with word?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Good point, I had made that assumption. The command can be grabbed programatically using
        my @Cmd = FetchCommand('.doc');
        assuming that file association is setup for Microsoft Word and the .doc file extension.
      Hi, my comment arrives a bit late for the discussion but I found this thread while looking for help about Win32::Process and want to help somebody else who read this later :-).

      If you want to start a process with Win32::Process you need to pass full-qualified executable name, which is usually unknown. In this case Win32::SearchPath is helpful. Find out the path to the executable:

      my $cmd = Win32::SearchPath::SearchPath('ipconfig');
      The module checks either the path in the same way as Windows does or let you specify directories where to look:
      my $cmd = Win32::SearchPath::SearchPath('ipconfig', 'C:\Windows');

      Then you start the process as you want using Win32::Process:

      Win32::Process::Create( $ProcessObj, $cmd, '/all', 0, IDLE_PRIORITY_CL +ASS, "." ) || die "error running process";
Re: system command
by CountZero (Bishop) on Mar 31, 2010 at 12:58 UTC
    The most flexible solution is to use Win32::OLE. Not only does it allow you to open the document, it gives you full control over the application as well to further process the file.

    Assuming that $wordoutput contains the filename of the file to open:

    my $document = Win32::OLE ->GetObject($wordoutput) or die Win32::OLE-> +LastError, $!;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: system command
by rovf (Priest) on Mar 31, 2010 at 11:57 UTC
    "open with ..." lets the user choose from a list of applications (i.e. openwith does NOT know whether to start, say, Word or OpenOffice or whatever), so IMO it doesn't make much sense, to say "start with WORD *like* openwith". They are different concepts, so you should make up your mind what you want.

    If you know that on the host your program is going to run, an association is set up between document files and the WORD application, you can just "run" the name of the word document:

    system("yourdocument.doc");
    If you want always to use Word, no matter what association has been set up, you do something like
    system("\\Your\\Path\\To\\Word\\winword.exe yourdocument.doc");

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: system command
by keszler (Priest) on Mar 31, 2010 at 13:10 UTC
    If you just want whatever association Windows might have for an extension to determine the program, and Windows "Command Extensions" are enabled, then
    system "start $filename";
    will do.
Re: system command
by Anonymous Monk on Mar 31, 2010 at 09:39 UTC
    system "/path/to/program", "path/to/file"

      That doesn't help much, as it shifts the problem towards determining the proper command line for the application to launch.