PHP COM MS Word Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Downugo
    New Member
    • Oct 2008
    • 5

    PHP COM MS Word Application

    This is my first post here - yay! This is my function that I am calling (just passing test arguments):

    Code:
    function makeDoc($name,$content){
    	// starting word
    	$word = new COM("word.application") or die("Unable to instantiate Word");
    	echo "Loaded Word, version {$word->Version}\n";
    	
    	//bring it to front
    	$word->Visible = 1;
    	
    	//open an empty document
    	$word->Documents->Add();
    	
    	//do some weird stuff
    	$word->Selection->TypeText("This is a test document. Your name is $name and you said: $content");
    	$word->Documents[1]->SaveAs("test.doc");
    	
    	//closing word
    	$word->Quit();
    	
    	//free the object
    	$word = null;
    }
    The output from the function is:
    Loaded Word, version 12.0
    Fatal error: Call to undefined method variant::SaveAs () in C:\wamp\www\... \docgen.php on line 93
    Also, I'm assuming that you must have Word installed on the machine? That will be fine for my localhost, but will I also have to put Word on the server when the time comes?

    Hey, thanks for your help!
    - Scott
    Last edited by pbmods; Apr 3 '09, 11:30 PM. Reason: Fixed CODE tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Scott. Welcome to Bytes!

    Firstly, the first index of a typical array is 0, so you would probably want to do:
    [code=php]
    $word->Documents[0]->SaveAs("test.d oc");
    [/code]
    Unless Microsoft somehow managed to mess that up as well.

    Originally posted by Downugo
    Also, I'm assuming that you must have Word installed on the machine? That will be fine for my localhost, but will I also have to put Word on the server when the time comes?
    To use this COM object on any server, that server will have to be running Windows, and it has to have Word installed.

    Comment

    • Downugo
      New Member
      • Oct 2008
      • 5

      #3
      Thanks Atli,

      I changed line 93 (now line 95, or line 14 from the snippet in my first post) so it now reads:

      $word->Documents[0]->SaveAs("test.d oc");

      I thought it was strange that it was accessing the second element of the array.

      Now when I call the function (on my local machine still) I get this:

      Loaded Word, version 12.0
      Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description :</b> The requested member of the collection does not exist.' in C:\wamp\www\... \docgen.php:95 Stack trace: #0 C:\wamp\www\... \docgen.php(95) : makedoc() #1 C:\wamp\www\... \docgen.php(25) : makedoc('Scott' , 'Time for winter.') #2 {main} thrown in C:\wamp\www\... \docgen.php on line 95

      Just in case it's relevant line 25 reads: makedoc("Scott" ,"Time for winter."); which is just where the function is called from.

      What can I fix?

      Thanks again,
      - Scott

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Scott.

        I looked over the PHP manual page for COM (http://php.net/manual/class.com.php), and I noticed a snippet about 3/4 of the way through the comments that used ActiveDocument instead of Documents[1].

        Worth a try....

        [code=php]
        $word->ActiveDocume nt->SaveAs(...);
        [/code]

        Comment

        • Downugo
          New Member
          • Oct 2008
          • 5

          #5
          Hey thanks pbmods, that is an awesome suggestions, and to me makes more sense the way you have suggested. After I made the change from

          $word->Documents[1]->SaveAs("test.d oc");

          to

          $word->ActiveDocume nt->SaveAs("test.d oc");

          it gave me a time a timeout error. When I added set_time_limit( 0); at the first of the function and set_time_limit( 30); at the end of the function it fixed the timeout error.

          Now when I run it (on my localhost still) I get this:

          Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.applicati on': Server execution failed ' in C:\wamp\www\... \docgen.php:90 Stack trace: #0 C:\wamp\www\... \docgen.php(90) : com->com('word.appl icatio...') #1 C:\wamp\www\... \docgen.php(29) : makedoc('Scott' , 'Time for winter.') #2 {main} thrown in C:\wamp\www\... \docgen.php on line 90

          Thanks for your help! It shouldn't be this hard! Ha ha.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Which line is line 90?

            Comment

            • Downugo
              New Member
              • Oct 2008
              • 5

              #7
              That is the line:

              $word = new COM("word.appli cation") or die("Unable to instantiate Word");

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                From what I can find from searching the web, it doesn't look like there's a problem with your code. My guess is that it either the COM extension or Microsoft Word is not behaving properly

                I'd try reinstalling (upgrading if applicable) PHP and/or Word on your server and see if that helps.

                Comment

                • Downugo
                  New Member
                  • Oct 2008
                  • 5

                  #9
                  Hey thanks, that is a great suggestion. I've been staring at the code and looking everywhere for examples and the code looks fine to me. It must be something in the configuration of either word or the configuration of php. I'll look into things further from both of those suggestions. Thanks!

                  Comment

                  • sukhvir13
                    New Member
                    • Apr 2009
                    • 2

                    #10
                    Facing same problem... Pls help someone

                    after all i am having error like
                    Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `ActiveDocument s': Unknown name. ' in C:\wamp\www\New Folder\index.ph p:12 Stack trace: #0 C:\wamp\www\New Folder\index.ph p(12): unknown() #1 {main} thrown in C:\wamp\www\New Folder\index.ph p on line 12


                    and on line 12...$word->ActiveDocument s[0]->SaveAs("Useles stest.doc");

                    Comment

                    • sukhvir13
                      New Member
                      • Apr 2009
                      • 2

                      #11
                      I written this code...
                      Code:
                      <?php
                      $word = new COM("word.application") or die ("Could not initialise MS Word object.");
                      $word->ActiveDocument->Open("doc1.doc");
                      // Extract content.
                      $content = (string) $word->ActiveDocument->Content;
                      echo $content;
                      $word->ActiveDocument->Close(false);
                      $word->Quit();
                      $word = null;
                      unset($word);
                      ?>

                      but it shows following error please help


                      Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description :</b> This command is not available because no document is open.' in C:\wamp\www\New Folder\index.ph p:3 Stack trace: #0 C:\wamp\www\New Folder\index.ph p(3): unknown() #1 {main} thrown in C:\wamp\www\New Folder\index.ph p on line 3
                      Last edited by pbmods; Apr 3 '09, 11:30 PM. Reason: Added CODE tags.

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, sukhvir13.

                        Try $word->Documents->Open() instead of $word->ActiveDocument .

                        If that doesn't work, you might be able to get by with $word->Documents->Add() and then $word->ActiveDocume nt->open().

                        There might be more info here:
                        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

                        Comment

                        • saimhe
                          New Member
                          • Mar 2012
                          • 1

                          #13
                          I had similar problems with some (but not all) methods being undefined. Final solution: "[x] Allow to interact with the desktop" among the properties of the Apache service. A real user instead of SYSTEM might also work.

                          Comment

                          • lgd1019
                            New Member
                            • Dec 2017
                            • 1

                            #14
                            Did you solve the problem? I also met the same problem

                            Comment

                            Working...