05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

This creates a reference (VT_BYREF) to a 32-bit integer (VT_I4) with an initial value of<br />

0. <strong>PHP</strong> can pass strings and numbers to COM servers automatically, but VARIANT<br />

COM types are required whenever you need to pass arguments by reference.<br />

For most OLE automation, the most difficult task is that of converting a VB method<br />

call to something similar in <strong>PHP</strong>. For instance, this is VBScript to insert text into a<br />

Word document:<br />

Selection.TypeText Text:="This is a test"<br />

The same line in <strong>PHP</strong> is:<br />

$word->Selection->Typetext("This is a test");<br />

It is important to note two quirks in <strong>PHP</strong>’s present COM support. First, you cannot<br />

pass parameters in the middle of an object method. So instead of writing a method<br />

as:<br />

$a->b(p1)->c(p2)<br />

you must break up the method as:<br />

$tmp=$a->b(p1);$tmp->c(p2);<br />

Second, <strong>PHP</strong> is unaware of default parameters from Microsoft OLE applications<br />

such as Word. This simply means that you must explicitly pass all values to the<br />

underlying COM object.<br />

Determining the API<br />

To determine object hierarchy and parameters for a product such as Word, you<br />

might visit the Microsoft developer’s site at http://msdn.microsoft.com/library/default.<br />

asp?url=/library/en-us/vbawd10/html/wotocObjectModelApplication.asp and search<br />

for the specification for the Word object that interests you. Another alternative is to<br />

use both Microsoft’s online VB scripting help and Word’s supported macro language.<br />

Using these together will allow you to understand the order of parameters, as<br />

well as the desired values for a given task.<br />

For instance, assuming we want to understand how a simple find and replace works,<br />

we can do the following:<br />

1. Open Word and create a new document containing some sample text. For<br />

example:<br />

"This is a test, 123"<br />

2. Record a macro to find the text “test” and replace it with the text “rest”. Do this<br />

by selecting Tools ➝ Macro ➝ Record New Macro from Word’s menu bar. Once<br />

recording, use search and replace to create the macro. We will use this macro,<br />

shown in Figure 15-2, to determine the values of parameters that we will pass in<br />

our <strong>PHP</strong> COM method.<br />

3. Use Word’s object browser to determine the calling syntax for all parameters in<br />

this example. Press Alt-F11 to access Word’s VBScript online help, then type in the<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Interfacing with COM | 361

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!