COM and MS Word Margins?
Posted: Tue Nov 09, 2004 3:15 am
Is it possible, after generating a MS Word file using COM, to set the margins of the MS Word document? If so how?
Thanks
Ben
Thanks
Ben
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
$content = "Insert Sample Text Here\n\nThis starts a new paragraph line.";
$word= new COM("word.application") or die("Unable to create Word document");
print "Loaded Word, version {$word->Version}\n";
$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = '3"';
$word->Selection->PageSetup->RightMargin = '4"';
$word->Selection->Font->Name = 'Helvetica';
$word->Selection->Font->Size = 8;
$word->Selection->Font->ColorIndex= 13; //wdDarkRed = 13
$word->Selection->TypeText("$content");
$word->Documents[1]->SaveAs("some_tst.doc");
$word->quit();
echo "done";
?>Code: Select all
<?php
include("msword.inc.php");
$rawpath = getcwd();
$root = strtr($rawpath, "", "/");
$root = $root."/";
$input = $root."template.htm";
$output = $root."template.doc";
$Word = new MSWord;
$Word->Open($input);
$Word->SetMargins("1","1","1.6");
$Word->SaveAs($output);
$Word->Quit();
unlink($finalinput);
?>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;
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);
}
// 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)
{
$word->handle->ActiveDocument->PageSetup->LeftMargin = '$left cm';
$word->handle->ActiveDocument->PageSetup->TopMargin = '$top cm';
$word->handle->ActiveDocument->PageSetup->BottomMargin = '$bottom 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
function fileread($filename)
{
$fd = @fopen($filename, 'r');
$filecontents = @fread ($fd, filesize($filename));
@fclose ($fd);
return $filecontents;
}Not actually str_replace, but instead Word's own find&replace function is used in the following example (VBA script, I seldom use PHP on windows boxes):Mr Tech wrote: Maybe you'll know how to generate a word doc from another word doc and still be able to use str_replace()?
Code: Select all
' this snippet replaces each 'sdf' with six exclamation signs
Dim w As Word.Application
Set w = CreateObject("word.application")
w.Visible = True
w.Documents.Open ("templ.doc")
w.ActiveDocument.Select
w.Selection.Find.ClearFormatting
w.Selection.Find.Replacement.ClearFormatting
w.Selection.Find.Text = "sdf"
w.Selection.Find.Replacement.Text = "!!!!!!"
w.Selection.Find.Forward = True
w.Selection.Find.Wrap = wdFindContinue
w.Selection.Find.Format = False
w.Selection.Find.MatchCase = False
w.Selection.Find.MatchWholeWord = False
w.Selection.Find.MatchWildcards = False
w.Selection.Find.MatchSoundsLike = False
w.Selection.Find.MatchAllWordForms = False
w.Selection.Find.Execute Replace:=wdReplaceAllCertainly. Now you have all you need to use .docs as a template.Mr Tech wrote:Would this help (Go to post by Justin):
http://www.progresstalk.com/printthread.php?t=25614