Re: Fw: COM surgery

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok, I see two main questions/problems..

1. An example of COM with Excel

2. How do you use COM with Word to set font size 20 and centered text?


Keep in mind that there may be better ways to solve the problems you have, but for the time being we'll use this as an excuse to play with COM.

#1 - There are at least 6 examples of using COM with Excel on php.net, so check there first.

Address: http://us2.php.net/manual/en/ref.com.php
Authors:
a dot kulikov at pool4tool dot com 13-Mar-2006 09:36
bruce at yourweb dot com dot au 23-Apr-2005 05:47
Jack dot CHLiang at gmail dot com 10-Apr-2005 10:02
markshane1 at fuse dot net 10-May-2004 04:41
dwt at myrealbox dot com 27-Aug-2002 03:31
madon at cma-it dot com 07-Mar-2002 12:59

#2. Again, check the page above and look for these authors:
pelegk2 at walla DOT co DOT il 13-Feb-2005 08:49
Shawn Coppock 11-Aug-2004 01:15
angelo [at] mandato <dot> com 03-Jun-2004 11:37
#no author name# 03-Oct-2003 12:55
php at dictim dot com 06-Sep-2003 01:17
admin at purplerain dot org 19-Apr-2002 04:34

Since you had a specific question about Word, let's see if we can give a quick example.

Starting with Shawn Coppock's example, because he already has the font size done, we just need to figure out how to do centering.

The easiest way to do this is to figure out how to do it in Word VBA (Visual Basic for Applications) then figure out the equivalent PHP.  The built in VBA editor for MS Office apps is actually really good for learning on.  Hit ALT-F11 to pull up the VBA editor, then right click on the tree on the left and do "INSERT MODULE".

Here's what I got in VBA:

Sub main()
  Selection.Font.Size = 20
  Selection.TypeText ("test")
  Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub

Took a minute to find the proper way to do 'center', but apparently it's a paragraph property.

Now using the PHP example...

<?php

define("wdAlignParagraphCenter", 1);

$content = "test";
$word= new COM("word.application") or die("Unable to create Word document");
print "Loaded Word, version {$word->Version}\n";
// TG: Changed this to 1 so you can see what's going on
$word->Visible = 1;
$word->Documents->Add();
$word->Selection->Font->Size = 20;
$word->Selection->TypeText("$content");
$word->Selection->ParagraphFormat->Alignment = wdAlignParagraphCenter;

$word->quit();
echo "done";
?>

Notice that I defined "wdAlignParagraphCenter".  Because in the VBA example, we just did "= wdAlignParagraphCenter".  This is a constant pre-defined inside Word.  Easiest way to figure out what the actual value is, is to do:

Selection.TypeText (wdAlignParagraphCenter)

This outputs "1".. therefore, defining PHP constant of the same name to be "1".

Sorry, don't have time to test this, but it should be close to correct.

One note about COM stuff in general.  After you run your script a few things, check your process list and make sure the application isn't still running in the background.  I forget exactly what it was, but if you don't close your COM application properly, it'll stay running and take up valuable system memory.

$word->close();  // Only closes the document and not the app?
$word->quit();   // Quits and closes application?

The only other tip I can give you is that in Excel, there are two ways of referring to cells.  One like "A1" (column A, row 1) and one is "1,1" (column 1, row 1).  I never found a way to use "A1" type notation via PHP.  You may be able to, but by default you're probably going to have to use "1,1" type notation.

This should be more than enough to get you going.  Check out the other examples, learn how to write VBA code inside Word and Excel then translate it to PHP.  Check out the lists of objects, properties, methods and collections on Microsoft.com listed under Word Object Model and Excel Object Model.

It will take a lot of trial and error before you get things right, probably.  But most of it's not too difficult if you get the basics down.

-TG

= = = Original message = = =

----- Original Message -----
From: <tg-php@xxxxxxxxxxxxxxxxxxxxxx>
To: <php-windows@xxxxxxxxxxxxx>
Sent: Wednesday, January 17, 2007 12:04 AM
Subject: Re:  Fw: COM surgery


> There's thousands of things you can access via COM.  Windows uses it
extensively.  Every application in Microsoft Office uses COM.  I've used COM
between Excel and MapPoint to generate maps (and yeah, I created a PHP
script to access MapPoint directly.. but it was just easier to do it via
Excel).
>
> Check out some of the examples in the PHP documentation:
>
> http://www.php.net/manual/en/ref.com.php
>
>
> Also, if you want to mess around with COM, try loading up MS Word or Excel
(I like Excel myself since you can use the cells to output your data and
keep it somewhat straight for debugging and general display).
>
> Also, MS Office apps have a fairly nice debugger that is aware of the COM
references you've set up so it will autocomplete methods and properties as
you type (even give you lists).  I found it easier to sketch out what I
needed in there first, then work on translating it to PHP.
>
> Another tip...  you won't just be dealing with methods (actions, 'verbs')
and properties (adjectives) but also objects will be returned (of various
types) and collections (of objects).
>
> Here's an example from mappoint:
>
> # Connect to MapPoint and define some objects
> $comobjMapPoint = new COM("mappoint.application") or die("Unable to
instantiate mappoint");
> $comobjMapPoint -> Activate();
> $comobjMapPoint -> Visible = FALSE;
> $mapobjActiveMap = $comobjMapPoint -> ActiveMap();
> $routeobjActiveRoute = $mapobjActiveMap -> ActiveRoute();
>
>
> Also.. you might find it helpful to search for the application you're
trying to connect to and the key words "object model", at least in the
Microsoft world.  Despite MS being kinda dumb sometimes, the Excel and
MapPoint object models online are actually really well documented.
>
thx for reply

i was gonna use com for word and excel
for excel.. i'm not using com(excel) since i don't have example related to
excel.. and then i created csv for read by ms Excel.. it kinda work for
now.. but the problem in csv is about the comma ","  or semi-column ";"
my country using ";" for seperated data
but in general using ","
if you have an example related to com(excel).. can u post in here

WORD...problem
sry if would be better if i put in new email..
i created a med-app using php.. Medical Check-Up document. the problem about
MCU is the clerk or the person who input data always lost the data and mess
up with everything.
in the past, they using word to type all kind patient data.

they use some kind templates where they always open then saveas new name and
then edit. if type for 5 person a day.. is no problem.. but 10-30
patient!?!??
they using name as file name like Joko bodo. but the person who named as
joko are not one (like mark in america).
i came up idea to build a form in php for their type.. leave the prog and
howto build form to me.. let's jump to the main problem.

the report must be print and doctor sometimes want to bring home the work
home. Then i have conclusion to create all data to a word..

the problem i have is.. format on the word.. some have big.. in center have
tab and soon
i need all example related to word.. like make the font are big (size 20)
and center like in htm

sry about my languages


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux