473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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



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
2 4507

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ..... | last post by:
I have an established program that I am changing to allow users to select one of eight languages and have all the label captions change accordingly. I have no problems with English, French, Dutch,...
4
by: Otis Hunter | last post by:
I have been fighting with this for days and your expert help is needed! Below is the code I am executing which results with "Object doesn't support this property or method". The error is occuring...
2
by: Todd | last post by:
Ok, this code works fine on the Web server machine. But when I try to run it via any other clients on the LAN, I keep getting scripting errors (the MS Word Document never opens.) Do I have to...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
1
by: Queenslander | last post by:
Hello, I have a form (Access 2003) which brings up a clients details on screen (name, phone number, etc etc), so that an end-user can modify any of these clients details, and save the updates....
6
by: arunbalait | last post by:
Hi.. I used web browser control or Ole control to open word dicuments. But whenever I opening the document I coulnd not save the document from VB itself. I want to save the opened...
7
by: Peter | last post by:
I have the following code which is executed from a modal web page - it works fine on most, but on some computers the Word loads into memory but never displays. This occurs with IE6 and IE7, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.