473,238 Members | 3,787 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,238 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 4498

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.