COM and MS Word Margins?
Moderator: General Moderators
COM and MS Word Margins?
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
didn't you ask about ms word and com awhile ago at
viewtopic.php?t=27003&highlight=word
check there agian for a list of resources.
viewtopic.php?t=27003&highlight=word
check there agian for a list of resources.
Yes, but none of those really links helped... I posted a new topic so it would be easier to find...
Anyway, I was searching and found some code:
I tried using the leftmargin and right margin but I just got the error:
Warning: Unable to lookup PageSetup: Unknown name. in c:\program files\easyphp\www\created\invoices\generate\generate.php on line 103
Any ideas on what's wrong? Is there another word for PageSetup?
Thanks
Ben
Anyway, I was searching and found some code:
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";
?>Warning: Unable to lookup PageSetup: Unknown name. in c:\program files\easyphp\www\created\invoices\generate\generate.php on line 103
Any ideas on what's wrong? Is there another word for PageSetup?
Thanks
Ben
Nope didnt work... I am using a class to do this script... maybe if you look at it tiw ill help.
generate.php
msword.inc.php
Hopefully that will help
?>
generate.php
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;
}
}
};
?>?>
$word->handle should be $this->handle, ='$var cm' should be ="$var cm" (type of quotes does matter). Do other methods work (such as creating, opening files, writing to the document)?
BTW, when MSWord 2003 records a macro, it uses InchesToPoints() function to convert the measures to the points. I'm not sure if it relevant here, but you may try to assign pure numbers instead of strings.
BTW, when MSWord 2003 records a macro, it uses InchesToPoints() function to convert the measures to the points. I'm not sure if it relevant here, but you may try to assign pure numbers instead of strings.
LOL while this topic is here I;ve just got a quick question.
When I generate the Word document, it is generated from a HTML document. I have done this because I needed to use str_replace() to repalce some [word] fields.
I couldn't seem to read a word document with COM() properly so I wasn't able to use str_replace(). I couldn't generate the word doc from another word doc because it wasn't able to read the contents of the word document using this function:
Maybe you'll know how to generate a word doc from another word doc and still be able to use str_replace()?
That may fix my next question below but I'll ask anyway:
In the HTML document I have pictures included. When I generate the Word document, the images go into it fine except when i save it onto another computer. The pictures then don't work. The only way I can get them to work is by linking to the images from my website... But when the computer isn't connected to the computer, they don't show up anymore plus when it is connected they need to load...
Any ideas on how I can make it read them without linking to the website.
When I generate the Word document, it is generated from a HTML document. I have done this because I needed to use str_replace() to repalce some [word] fields.
I couldn't seem to read a word document with COM() properly so I wasn't able to use str_replace(). I couldn't generate the word doc from another word doc because it wasn't able to read the contents of the word document using this function:
Code: Select all
function fileread($filename)
{
$fd = @fopen($filename, 'r');
$filecontents = @fread ($fd, filesize($filename));
@fclose ($fd);
return $filecontents;
}That may fix my next question below but I'll ask anyway:
In the HTML document I have pictures included. When I generate the Word document, the images go into it fine except when i save it onto another computer. The pictures then don't work. The only way I can get them to work is by linking to the images from my website... But when the computer isn't connected to the computer, they don't show up anymore plus when it is connected they need to load...
Any ideas on how I can make it read them without linking to the website.
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:=wdReplaceAllWould this help (Go to post by Justin):
http://www.progresstalk.com/printthread.php?t=25614
I also found this:
http://www.experts-exchange.com/Web/Web ... 06226.html
Tested it but the page just kept going and it wouldn't load...
http://www.progresstalk.com/printthread.php?t=25614
I also found this:
http://www.experts-exchange.com/Web/Web ... 06226.html
Tested it but the page just kept going and it wouldn't load...
Certainly. 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