开发者

Doc to PDF conversion using php and Word. How to handle unknown files?

We use an application.word COM object from PHP to convert Word files to PDF in the background.

This works quite well with the following code:

$this->com = new COM('word.application');
$this->com->Visible = false;
$this->com->DisplayAlerts = false;
$this->com->Documents->Open($from /* Filename */, false /* ConfirmConversions */,  
                            true /* ReadOnly */, false /* AddToRecentFiles */, 
                            '' /* PasswordDocument */, '' /* PasswordTemplate */, 
                            false /* Revert */, '' /* WritePasswordDocument */, 
                            '' /* WritePasswordTemplate */, 0 /* Format */, 
                            NULL /* Encoding */, true /* Visible */, 
                            false /* OpenAndRepair */, NULL /* DocumentDirection */, 
                            true /* NoEncodingDialog */, '' /* XMLTransform */);

(Exception handling left out for brevity)

However if word does not recognize the file-type it opens a dialog asking about the format, blocking our php script. As i understand the documentation here:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.wor开发者_如何学Pythond.documents.open%28v=Office.11%29.aspx

the second parameter, which is set to false, should suppress this dialog.

Is there any way to suppress this dialog and make word simply fail opening the file with e.g. an Exception?

We are using Word 2007 on the server in a virtual machine and the script is triggered by a web application, so simply manually pressing cancel on the dialog is not the best option...

EDIT: changed to com call as suggested in the first answer. Does not work though.


I think you need to read further on that page. You probably need the NoEncodingDialog parameter. After you supressed the dialog, I'm not sure if Open throws an exception. Could be that it returns null, so make sure to test which one it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜