By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

COM support: Using PHP to open a URL from within Word then save thedocumet

P: n/a


I am trying to use PHP's COM support to open a URL from within MS Word
then save the document.

I am using PHP5.0.3/Apache2/WindowsXP. phpinfo() confirms that COM support
is enabled.
Manually, this would be:

- Start MS Word (am using Office 2003)
- File > Open
- Enter a URL and click Open
- File > Save As...
- Save the file then close MS Word.
Does anyone know the COM syntax used in PHP to open a URL (rather than a
file) with Word?

Using
$this->handle->Documents->Open('http://www.example.com/page/');

seems not to work.

I have tried two approaches, neither of which work.
1) The posting at http://uk.php.net/com made at
03-Jun-2004 07:37
Using example 1, but changing the input to a URL rather than a file (as
above).
2) The posting at
http://www.phpbuilder.com/lists/php-...03082/0067.php
suggests that the following outght to work:

$word=new COM("Word.Application") or die("Cannot start word for you");

$word->visible = 0;
$word->Options->ConfirmConversions = false; // menu:
options->general->confirm conversions
// search for HTML Converter
$fcList = $word->FileConverters();
$count = $fcList->Count();
for($i=1; $i<=$count; $i++) {
$fc = $fcList->Item($i);
// go for ClassName, it's locale-independent
if ($fc->ClassName() == 'HTML') break;
}
if ($i <= $count) {
$word->Documents->Open ($inputUrl, False, False, False, '', '', False, "", "", $fc->OpenFormat());
$word->Documents[1]->SaveAs($outputWordFile, wdFormatDocument);
$word->Document[1]->Close();
$word->Quit();
$word->Release();
}

but it does not. The script runs, but causes zero errors (error_reporting
is set to 2047), yet does not output any file.

Any suggestions?
Martin
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Martin:

[Mildly off-topic but CLP does get occasional COM/PHP queries and this
might be useful]

Your second example is fine, except you have no execution path if there
is no file-converter class called "HTML". I dumped a list of the class
names and there's no such class - perhaps the example relates to an
older version of Word.

Instead, specify that Word should use the default converter (constant
wdOpenFormatAuto, value 0) in the Open() call. I found this by
performing the File/Open manually with Macro Recording turned on, then
examining the code that Word used to get the document.

Additionally, I got a complaint from COM about the SaveAs() call:

PHP Warning: (null)(): Unable to lookup Document: Unknown name.

(Although the file is still saved successfully). If you decode the
ActiveDocument and use that instead of Documents[1] then there are no
warnings:

<?

define( 'wdOpenFormatAuto', 0 );
define( 'wdFormatDocument', 0 );

$inputUrl = 'http://www.google.com/';
$outputWordFile = 'c:\temp\test.doc';

$word = new COM("Word.Application") or die("Cannot start word for
you");
$word->visible = 0;
$word->Options->ConfirmConversions = false; // menu:
options->general->confirm conversions
$word->Documents->Open( $inputUrl, False, False, False, '', '',
False, "", "", wdOpenFormatAuto );
$doc = $word->ActiveDocument();

$doc->SaveAs($outputWordFile, wdFormatDocument);
$doc->Close();

unset($Doc);
$word->Quit();
$word->Release();

unset($word);

?>
---
Steve

Jul 17 '05 #2

P: n/a

On Mon, 10 Jan 2005, Steve wrote:
Martin:

[Mildly off-topic but CLP does get occasional COM/PHP queries and this
might be useful]

Your second example is fine, except you have no execution path if there
is no file-converter class called "HTML". I dumped a list of the class
names and there's no such class - perhaps the example relates to an
older version of Word.
Ah yes, good point.

After a bit of fiddling, I managed to get this working, based on the
example you gave below - thanks. One question, though:

I'm curious as to why the syntax here is the slightly strange

define( 'wdFormatDocument', 0 );
$doc->SaveAs($outputWordFile, wdFormatDocument);

rather than just

$doc->SaveAs($outputWordFile, 0);

(which in PHP ordinarily would surely amount to the same thing), or

$doc->SaveAs($outputWordFile, 'wdFormatDocument');

Is there something 'special' here about assigning a particularly-named
constant, named the same as the flag required as noted in the API
documentation (as gleaned from VB or MSDN)?
<?

define( 'wdOpenFormatAuto', 0 );
define( 'wdFormatDocument', 0 );

$inputUrl = 'http://www.google.com/';
$outputWordFile = 'c:\temp\test.doc';

$word = new COM("Word.Application") or die("Cannot start word for
you");
$word->visible = 0;
$word->Options->ConfirmConversions = false; // menu:
options->general->confirm conversions
$word->Documents->Open( $inputUrl, False, False, False, '', '',
False, "", "", wdOpenFormatAuto );
$doc = $word->ActiveDocument();

$doc->SaveAs($outputWordFile, wdFormatDocument);
$doc->Close();

unset($Doc);
$word->Quit();
$word->Release();

unset($word);

?>

Martin
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.