<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Developer Tutorials' Webmaster Blog &#187; php tricks</title>
	<atom:link href="http://www.developertutorials.com/blog/tag/php-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developertutorials.com/blog</link>
	<description>Keeping webmasters up-to-date on technology.</description>
	<pubDate>Tue, 02 Sep 2008 14:59:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Extracting text from Word Documents via PHP and COM</title>
		<link>http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/</link>
		<comments>http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 08:00:49 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[php COM]]></category>

		<category><![CDATA[php tips]]></category>

		<category><![CDATA[php tricks]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/</guid>
		<description><![CDATA[I was recently working on an enterprise project in which I needed to detect the text inside a Word Document. Now, I could have got rid of all the non-standard characters from the .doc file and hoped I got something reasonable at the end. I could have tried to run Word 2007 via command line [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on an enterprise project in which I needed to detect the text inside a Word Document. Now, I could have got rid of all the non-standard characters from the .doc file and hoped I got something reasonable at the end. I could have tried to run Word 2007 via command line to save the file as a .docx. Or I could just talk to any copy of MS Word via COM and have it do all the dirty work for me.</p>
<p>Naturally, I chose the latter. Here&#8217;s ten lines of code that do just that.</p>
<p><span id="more-81"></span></p>
<p>Communicating via COM in PHP is easy as ever; especially for people coming from a VB background where executing complex tasks in MS-applications is a piece of cake, you will feel right at home in PHP. In fact, VB COM calls can be converted to PHP COM calls in just a few simple search and replaces.</p>
<p>Without further ado, here&#8217;s the code. I&#8217;ll point out a gotcha I noticed in just a moment.</p>
<blockquote><pre>&lt;?php
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));

// Extract content.
$content = (string) $word-&gt;ActiveDocument-&gt;Content;

echo $content;

$word-&gt;ActiveDocument-&gt;Close(false);

$word-&gt;Quit();
$word = null;
unset($word);</pre>
</blockquote>
<p>We first create a new COM object of word.application, which provides access to core MS Word functionality. We then tell it to open &#8220;Sample.doc&#8221; in the current directory. There&#8217;s a bit of a bug/feature when trying to get the content out, however. If you debug this code, you&#8217;ll find that $word-&gt;ActiveDocument-&gt;Content is an empty object (variant). If you assign the value to a variable you&#8217;ll get an empty string, as the variant object has no real __toString(). The workaround in PHP is to explicitly type cast the value as a string and make PHP/COM take care of finding the real value. If you try this in a VB macro run inside Word, this is not neccessary - a MsgBox(ActiveDocument.Content) works fine.</p>
<p>We also need to be aware of performance considerations on Windows servers - creating a COM object initialises a fully-fledged instance of WINWORD.exe, and a 10-15MB memory footprint associated with it. We first call the Quit() method on the Word COM object, then null the variable and destroy it to be safe. Watch your task manager while running this script and you&#8217;ll notice WINWORD.exe appears briefly then exits.</p>
<p>So, in just ten lines of code you can get the text out of an MS Word document, easy as ever!</p>
<p>If you want to explore this approach further, load up MS Word and open the Visual Basic Script Editor - press Alt+F11, then F2. (If that doesn&#8217;t work, Tools > Macros > Visual Basic Script Editor, or &#8220;Visual Basic&#8221; under the Developer tab 2007, then press F2.) From the library drop-down box in the &#8220;Object Browser&#8221; - which might say &#8220;<All Libraries>&#8221; by default - select Word. Just about everything you see there is available via COM in PHP.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark" style="float:left;">
<div class="d81" style="overflow:hidden">
<div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/&amp;title=Extracting+text+from+Word+Documents+via+PHP+and+COM" title="Add to&nbsp;Del.icio.us">Del.icio.us</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/&amp;title=Extracting+text+from+Word+Documents+via+PHP+and+COM" title="Add to&nbsp;Digg This">Digg This</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/&amp;title=Extracting+text+from+Word+Documents+via+PHP+and+COM" title="Add to&nbsp;Stumble">Stumble</a></div></div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/extracting-text-from-word-documents-via-php-and-com-81/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
