{$errormessage}

\n\n"; } $word = new com( "Word.Application" ) or die( $errormessage ); $wordversion = "[Microsoft Word, Version {$word->Version}]"; if ( !$commandline ) { $wordversion = "

{$wordversion}

\n\n"; } print( $wordversion ); try { $word->Visible = 1; $document = $word->Documents->open( $wordfile ); if ( $commandline ) { print( "[{$_POST['filename']}]\n\n" ); } else { print( "

[{$_POST['filename']}]

\n\n" ); } if ( $word->Documents->count( ) == 1 ) { foreach ( $document->Sentences as $sentence ) { if ( $commandline ) { print( "{$sentence}\n\n" ); } else { print( "

{$sentence}

\n\n" ); } } } } catch ( Exception $e ) { if ( $commandline ) { print( "{$e->getMessage( )}\n\n" ); } else { print( "
{$e->getMessage( )}
\n" ); } } finally { if ( $word->Documents->count( ) > 0 ) { $word->Documents->close( ); } $word->Quit( ); $word = null; } } function show_help( ) { global $progver; print( "\nreadword.php, Version {$progver} for Windows\n" ); print( "Read a Word document and display the text in a console or web page\n\n" ); print( "Usage: PHP \"" . $_SERVER['SCRIPT_NAME'] . "\" word_doc\n\n" ); print( " or: [ START ] http://example.com/" . basename( __FILE__ ) . "\n\n" ); print( "Where: word_doc is the full path to an MS Word document\n" ); print( " http://example.com/" . basename( __FILE__ ) . " is the url to this script.\n\n" ); print( "Notes: When run on the command line, this script requires a Word document\n" ); print( " as its only command line argument; when run in a browser, the file\n" ); print( " name has to be entered interactively.\n" ); print( " In command line mode only the first argument is used, any excess\n" ); print( " arguments will be ignored.\n" ); print( " This Windows-only script requires MS Word to read the Word document.\n\n" ); print( "Written by Rob van der Woude\n" ); print( "https://www.robvanderwoude.com\n" ); } if ( $commandline ) { array_shift( $argv ); if ( count( $argv ) == 0 || $argv[0] == "/?" ) { show_help( ); } else { read_word( $argv[0] ); } } else { print( "\n" ); print( "\n" ); print( "Read Word document in web page with PHP by Rob van der Woude\n" ); print( "\n" ); print( "\n\n" ); print( "
\n" ); print( "\n" ); if ( isset( $_POST ) && isset( $_POST['loadfile'] ) && $_POST['loadfile'] == '1' && isset( $_POST['filename'] ) && trim( $_POST['filename'] ) != '' ) { read_word( $_POST['filename'] ); } else { print( '

Word file name: ' ); print( '' ); print( ' ' ); print( '

' ); } print( "
\n\n" ); print( "\n" ); print( "" ); } ?>