I am trying to use PHP with word.application to read a file. It simply will not open the file. It's echoing the right version.
$w = new COM("word.application") or die("Is office installed?");
echo 'Loaded Word, version ' . $w->Version . '<br>';
$w->Visible = false;
$w->Documents->Open(realpath('test.docx'));
$content = (string) $w->ActiveDocument->Content;
echo $content;
$w->Quit();
$w->Release();
$w = null;
I get the error:
Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.'
It feels like it's some kind of permission problem. I tried to put the path of the test.docx besides using realpath and that did not help. Also tried to put it in the root of my C drive. I am using Windows 7 Professional and Microsoft Office 2007.
Documents->Open returns a document if all is ok. Probably, the document does not exists (path incorrect), or you haven't got the rights to open it from PHP. Store the result in $var, check if it has an appropriate value (probably not isset, null or false if not), and use $var->Content to read the content.
Try doing a file_exists on said
file/path.
If that works, try
file_get_contents and see if you can
read it.
If that all works - then it's not a problem with permissions/etc.
Related
I have been given 2 dll's.
The first encrypts a string.
The second, decrypts an encrypted string.
I do not know how it works this dll.
Also I have been given a code example in .net (Don't know .net)
dll_decrypt_.Decrypt myuser = new dll_decrypt.Decrypt();
user = myuser.DecryptString(userEncrypted);
I have searched among the web, but still cant' find the answer.
Also I have added the 2 dll's to the ext folder in wamp:
C:\wamp64\bin\php\php7.0.32\ext
And added the "extension=dll_decrypt.dll" into the php.ini file both in apache and php folders.
This would be my php code:
try
{
$dll = new COM('dll_decrypt.Decrypt');
$dll->Function();
}
catch(Exception $e)
{
echo 'error: ' . $e->getMessage(), "\n";
}
I expect to get the decrypted string so I can see the user.
I am using a PHP COM Object to Open a Word 2013 file and count pages. My code works great for .docx files but not for .docm files. I am using php 5.4 and IIS 7.5 in a Windows 2008 R2 environment. Any recommendations?
// Create an instance of the Word application
$word = new COM("word.application");
if ($word) {
// Open the document
$word->Documents->Open($file_dir . $filename);
// Get the page count
$pagecount = $word->Documents[1]->ComputeStatistics(2);
}
You could try to turn off all macros like this before opening a file, this also disable security prompts:
$word->AutomationSecurity = 3; // disable all macros in the documents being opened
Well, doesn't .docm signify that there are macros or event macros present in the file?
Usually when the user opens a .docm file, the user is given a security prompt. Being that you are running under PHP, you might be running under a service and the prompt is not being seen in the hidden desktop, or security it preventing it from running at all. I would suggest you write some VBS code to try and open the same file under a console and see what the results are. If you get prompts or its being prevented, hopefully you will get better error messages.
var word
set word = CreateObject("Word.Application")
word.documents.open("path to my file")
var pageCount
pageCount = word.Documents(1).ComputeStatistics(s)
I'm using xampp in Windows for local developing environment. I tried to create a simple logger which appends to a text file.
function Logger ($logmessage)
{
$filename = '/errorlog-' . date('Ymd') . '.txt';
file_put_contents($filename, $logmessage, FILE_APPEND | LOCK_EX);
}
I've tried to echo the $filename, and it says '/errorlog-20140427.txt' which means it already has valid filename (I think).
But when I call this logger function, there's no error raised, but I can't find the file everywhere. I tried to search for the whole htdocs for *.txt but no files found. Do you know why I can't write file using php? Do I need to use fopen first? As I refer to another help, I can just use file_put_contents directly without fopen. Thanks for the help.
You're trying to write to the root of your filesystem, e.g.
$file = '/error-log-' etc...
is essentially the same as
$file = 'c:/error-log' etc...
The webserver account is highly unlikely to have the rights to do anything in C:\.
If you'd bothered checking the return value of file_put_contents, you'd have seen it was returning a boolean false to indicate failure.
Microsoft Excel cannot access the file
'C:\xxx\test.xls'. There are several possible reasons: • The file name
or path does not exist. • The file is being used by another program. •
The workbook you are trying to save has the same name as a currently
open workbook.
I followed this steps
link
Also I included the extesion "php_com_dotnet.dll" in php.ini.
I have Zend Server CE with php 5.3.14
The issue persist when I reboot the computer.
code::
$file = "C:\\xxx\\test.xls";
try {
$excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!\r\n");
$excel->Visible = true;
$Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
} catch (Exception $exc) {
echo $exc->getMessage();
Things to consider:
Have you checked the permissions of the file? I'm assuming "xxx" is a placeholder for the proper folder name as well?
Do you have file open by something else? Excel perhaps? Excel will grab the file and "lock" it down from use by other things.
What user is the web server running as? Can it access that path?
try the answer from this guy named Aardigspook
in https://www.excelforum.com/excel-general/1182105-excel-cannot-access-the-file-2.html
The problem is a 'Name' . Go to the 'Formulas' tab, click 'Name Manager' and scroll down to the entry named 'special'. It refers to '--filepath--/[xxxxx.xlsx]Sheet1'!$A:$A'. Delete this, or amend it to a reference you have/want, and the error should be gone.
-captain hair.
After searching for a week and 3 times a new office 365 installation, my solution was to replace my faulty Micro-SD adapter.
I am trying to follow a tutorial on converting doc to pdf using openoffice. I have the following code:
<?php
set_time_limit(0);
function MakePropertyValue($name, $value,$osm){
$oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
}
function word2pdf($doc_url, $output_url){
// Invoke the OpenOffice.org service manager
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
// Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
// Launch the desktop
$top = $osm->createInstance("com.sun.star.frame.Desktop");
// Load the .doc file, and pass in the "Hidden" property from above
$oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args);
// Set up the arguments for the PDF output
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
// Write out the PDF
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
}
$output_dir = './';
$doc_file = './test.docx';
$pdf_file = 'DpmR5Reqv1.20.pdf';
$output_file = $output_dir . $pdf_file;
$doc_file = 'file:///' . $doc_file;
$output_file = 'file:///' . $output_file;
word2pdf($doc_file,$output_file);
?>
I get the error:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `com.sun.star.ServiceManager' in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
( ! ) com_exception: Failed to create COM object `com.sun.star.ServiceManager': Invalid syntax in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
Ive tried to what this tutorial suggests: http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/ But no luck. Any idea what I can do? I am running this under wamp and it will be ran under wamp in production.
You have to have OpenOffice setup to run as a service on that machine. To simply convert an odt to a pdf you can use pyodconverter. They also explain how to setup the local OpenOffice service:
http://www.artofsolving.com/opensource/pyodconverter
I'm using this technique in a script I wrote and have an article for here:
http://codeuniversity.com/scripts/scr1
Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.
it is much easier to use headless libreoffice and a php wrapper class like https://github.com/ncjoes/office-converter.
of course you have to install libreoffice and you must have full control of your webserver.