Page 1 of 1
PHP, Microsoft Word and Page Breaks?
Posted: Tue Feb 13, 2007 11:31 pm
by Mr Tech
I use PHP to open, replace fields and save Microsoft Word documents...
When I replace fields, I use the following code that is executed by a class:
Code: Select all
$Word->Replace("[field_name]", $field_value);
What I want to do is add "page breaks" to a couple of the replace fields. here are some exampels:
Code: Select all
$Word->Replace("[field_name]", $field_value."<br>".$field_value2);
Code: Select all
$Word->Replace("[field_name]", $field_value."\n".$field_value2);
Code: Select all
$Word->Replace("[field_name]", $field_value."\r".$field_value2);
However, <br> doesn't work and \n and \r add those little squares.
Is it possible to add a page break to a word document?
Posted: Wed Feb 14, 2007 12:05 am
by Kieran Huggins
You'd likely need to consult the word class' documentation - DOC format is a proprietary format AFAIK.
I'd be interested to know the source of the class, if it's available!
Posted: Wed Feb 14, 2007 12:11 am
by Mr Tech
I wouldn't have a clue where I got it from and it doesn't have any copyright in the file... Maybe from phpclasses.org?
Here it is. Does the code give you any ideas about the page breaks?
Code: Select all
<?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;
}
}
};
?>
Code: Select all
$Word = new MSWord;
$Word->Open("document.doc");
$Word->SaveAs("document2.doc");
$Word->Quit();
Posted: Thu Apr 05, 2007 2:27 pm
by Port
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.
Hope this helps.
BTW: Application.ActiveDocument.Sections.Add
Port
Posted: Thu Apr 05, 2007 3:58 pm
by RobertGonzalez
That was an excellent mini tutorial on how to see that stuff. Thanks for that.