0

Reading MS-Word contents in php

It has been long since written on my blog, how lazy I m, why not get some treatment form the people around me. Why not developers all around chop ma head and screw me up for being so lazy. But mind you don’t try this, coz I belong to the area where people are treated so hard that nobody can even think of. Now don’t ask me where do I belong to.

Its Saturday, off, but I m on and m in great mode. I don’t know why m I feelen so happy though we lost our semi last night. I’m juz chilling.

Gaad daym, what I am written on my technical blog, it shon’t be like this. So lemme move to the topic and put aside this mess and hassles.

Since yesterday I’m try to get this work for me, I’s unable yesterday, but today it really worked for me. Nothing else but COM did the job for me. I m going to share this code with you and m sure that it will serve you lot. Don’t tell anyone that I found this code on the web otherwise I will punch you down and kick you skinny ass.

The code here will read contents form .doc, save those to .txt file and then read contents from that txt file. Here is the code.

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

$word->Documents->Open($file);

$new_filename = substr($file,0,-4) . ".txt";

// the '2' parameter specifies saving in txt format

$word->Documents[1]->SaveAs($new_filename,2);

$word->Documents[1]->Close(false);

$word->Quit();

//$word->Release();

$word = NULL;

unset($word);

$fh = fopen($new_filename, 'r');

// this is where we exit Hell

$res = fread($fh, filesize($new_filename));

fclose($fh);

unlink($new_filename);

echo $res;

So what we’re doing here.

Instantiate COM, open file, save content as .txt file, open .text file and get contents. That’s it.

Juz give it a try and you will find it quite helpful.

Filed in: PHP Tags: , ,

Leave a Reply

Submit Comment

© 7487 Faheem Abbas. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
Top Footer