Reading Word 2007 Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gobro
Forum Newbie
Posts: 4
Joined: Mon Sep 07, 2009 4:23 pm

Reading Word 2007 Files

Post by Gobro »

Hello,

I'm trying to read some .doc or .docx files or make any contact with word 2007 but so far i'm getting nothing, i tried to get it working even used the example code from php.net but it still refuses to work with word, i don't know why.

The example code from php.net

Code: Select all

<?php
// 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...");
$word->Documents[1]->SaveAs("Useless test.doc");
 
//closing word
$word->Quit();
 
//free the object
$word = null;
?>
Fatal error: Call to undefined method variant::SaveAs()
or
Fatal error: Call to undefined method variant::Quit()

Also tried

Code: Select all

$content = (string) $word->ActiveDocument->Content;
echo $content;
And various stuff with

Code: Select all

$word->ActiveDocument
$word->Documents[1]
$word->Documents[0]
$word->Documents
All i'm getting is function not found or an infinite time loop until i kill the word process... :(
Gobro
Forum Newbie
Posts: 4
Joined: Mon Sep 07, 2009 4:23 pm

Re: Reading Word 2007 Files

Post by Gobro »

Anyone?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Reading Word 2007 Files

Post by Eran »

I would suggest against using COM objects, it would only work on windows servers. It is also somewhat tedious to use. We use AbiWord http://www.abisource.com/ which is an open-source solution for reading / converting word documents into many formats (including HTML).
Gobro
Forum Newbie
Posts: 4
Joined: Mon Sep 07, 2009 4:23 pm

Re: Reading Word 2007 Files

Post by Gobro »

It's not even on a server i'm trying it on my local machine and i just want to read some words docs... :(
Gobro
Forum Newbie
Posts: 4
Joined: Mon Sep 07, 2009 4:23 pm

Re: Reading Word 2007 Files

Post by Gobro »

Thanks a lot for not replying everyone...
Switched to TXT files.
phaeton
Forum Newbie
Posts: 5
Joined: Wed May 03, 2006 3:02 pm

Re: Reading Word 2007 Files

Post by phaeton »

I though pytrin reply was quite informative. He mentnioned using COM objects but that will only work on a Windows machine.

I'm glad you've found a solution that works for you.
Post Reply