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!
<?php
// msword.inc.php
// NOTE: Using COM with windows NT/2000/XP with apache as a service
// - Run dcomcnfg.exe
// - Find word application and click properties
// - Click the Security tab
// - Use Custom Access Permissions
// - Add the user who runs the web server service
// - Use Custom Launch permissions
// - Add the user who runs the web server service
$wdFormatDocument = 0;
$wdFormatTemplate = 1;
$wdFormatText = 2;
$wdFormatTextLineBreaks = 3;
$wdFormatDOSText = 4;
$wdFormatDOSTextLineBreaks = 5;
$wdFormatRTF = 6;
$wdFormatUnicodeText = 7;
$wdFormatHTML=8;
$wdReplaceAll = 2;
$wdFindContinue = 1;
class MSWord
{
// Vars:
var $handle;
// Create COM instance to word
function MSWord($Visible = false)
{
$this->handle = new COM("word.application") or die("Unable to instanciate Word");
$this->handle->Visible = $Visible;
}
// Open existing document
function Open($File)
{
$this->handle->Documents->Open($File);
}
function Replace($what, $with) {
$this->handle->Documents[1]->Activate();
$this->handle->ActiveDocument->Select();
$this->handle->Selection->Find->ClearFormatting();
$this->handle->Selection->Find->Replacement->ClearFormatting();
$this->handle->Selection->Find->Execute($what, false, false, false, false, false, true, 1, false, $with, 2, false, false, false, false);
}
// Create new document
function NewDocument()
{
$this->handle->Documents->Add();
}
// Write text to active document
function WriteText( $Text )
{
$this->handle->Selection->Typetext( $Text );
}
// Set page margins
function SetMargins($top,$bottom,$left,$right)
{
$this->handle->Documents[1]->PageSetup->TopMargin = "$top cm";
$this->handle->Documents[1]->PageSetup->BottomMargin = "$bottom cm";
$this->handle->Documents[1]->PageSetup->LeftMargin = "$left cm";
$this->handle->Documents[1]->PageSetup->RightMargin = "$right cm";
}
// Number of documents open
function DocumentCount()
{
return $this->handle->Documents->Count;
}
// Save document as another file and/or format
function SaveAs($File, $Format = 0 )
{
$this->handle->ActiveDocument->SaveAs($File, $Format);
}
// Save active document
function Save()
{
$this->handle->ActiveDocument->Save();
}
// close active document.
function Close()
{
$this->handle->ActiveDocument->Close();
}
// Get word version
function GetVersion()
{
return $this->handle->Version;
}
// get handle to word
function GetHandle()
{
return $this->handle;
}
// Clean up instance with word
function Quit()
{
if( $this->handle )
{
// close word
$this->handle->Quit();
// free the object
$this->handle->Release();
$this->handle = null;
}
}
};
?>
There are classes that will allow you insert different types of breaks. Open MS Word go to
tools->macro->visual basic editor.
on the toolbar click the object browser (a button containing an image of a yellow box with blue circle and a red square above it).
On the window that apears click on the first drop down select box (reads [ <ALL Libraries> ]). Choose "Word".
All the word classes will show up. From here you will be able to see all the classes the make up word and how to maniputlate them using the PHP COM object. Also, if you right-click on any of the classes and choose help, it gives you a thorough doc with examples of that particular class, method, enum, property, etc. You will have to install the help the first time you use it.