Copy link to clipboard
Copied
I am a complete newbie to ColdFusion and Flex. There was a Flex and Coldfusion developer in our project who left so that work has come to me. My programming background is mainly in C,C++, UNIFACE, PL/SQL with some HTML, JavaScript.
I have a couple of questions and would appreciate if someone could respond to them.
The application I am expected to maintain and enhance prints out reports in PDF format which we need to be in MS-Word format. I have looked at the source for the application and see the below lines in one file
<cfdocument format="pdf" filename="#report_filename_format#" orientation="landscape"
pagetype="legal">
Is there a way to do it in the program so that the output is a MS-Word document? I understand the format attribute can take only "PDF" or "flashpaper" as values as per http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFu....
I looked at the example at http://tutorial13.easycfm.com/ which shows how you can generate a Word document using ColdFusion.
The first time I ran it it worked well and a word document called Hello.doc was created. The next time, I changed the text to be printed in the Word document from "Hello World" to "Hello World again" it did not and I got the error message "Error Occurred While Processing Request
The SaveAs method was not found. There are no methods with the specified method name and argument types. Check your function and try again. "
I understand the first time, there was no Word document application object so it was created and the text was written to it properly. The next time, ColdFusion is trying to connect to the Word application object which exists, but somehow it is failing. I changed the name of Word file to be written from Hello.doc to Hello123.doc, but it still did not work. I have permissions to write in the directory where the Word file is being created which is why the example ran fine the first time.
The way I am running this example is typing code in a file called helloworld.cfm which is present in my C:\ColdFusion8\wwwroot\CFIDE\administrator\ directory using Editplus editor, then opening a browser and trying to open a page http://127.0.0.1:8500/CFIDE/administrator/index.cfm?target=/CFIDE/administrator/helloworld.cfm?
1. Is this the proper method?
2. Can someone please clarify where I might be erring in running this example and how can I fix it?
3. Is there an easier way to get the output from ColdFusion in MS-Word format than what I am doing?
Thanks a lot.
Copy link to clipboard
Copied
1) Depends on your definition of 'proper'. It is a method, that can work but it is very fragile. The problem is that Microsoft Word is not an application that was ever meant to be run remotely on a server. It is chock full of events that will result in a dialog box being gerenarated on the server that can only be responded to by a user logged directly into the server desktop. Once this happens the MS-Word object is locked and will continue to throw errors until the dialog is responded to. Remotely all you can do is to connect to the server and kill the word process from the task manager and try again.
2) A lot of trial and error with the above romote connection to frequently kill the word process when you have yet again locked up the word process.
3) A much easier way is to not try for a direct MS-Word format document. That is a format Microsoft is not very forth comming in letting other tools create. But it is almost never necessary to actually create a MS-Word ".doc" file to get the actual functionality desired by somebody. Usually people just want a document that can be opened by MS-Word and look the way they want it to look.
This can almost always be done more easily with the rich text ".rtf" format which is much more open and is just text manipulation, rather then trying to create a binary file for the .doc format.
What I have done in the past is to create a boiler plate of what the final document should be in MS-Word, save it to rich text, then open that in a file viewer that shows all the code, not just the formated text. It is usually pretty easy to see what code needs to be progammed to start making dynamic documents from this template.
HTH
Ian
P.S. IF you developing your application in the CFIDE/Administrator directory, this is usually a very undesirable location. The CFIDE is the ColdFusion directory for it's administration portal and other CF functionality. One would usually put their application into the web root of the web server configured to work with ColdFusion.
Copy link to clipboard
Copied
1. Is this the proper method?
No. As Ian has mentioned, you shouldn't be writing any code anywhere in /CFIDE. It is one of Coldfusion's system directories. We have to eat in the restaurant, not in the kitchen.
2. Can someone please clarify where I might be erring in running this example and how can I fix it?
There is another technical consideration. There are a number of system Application.cfm files in CFIDE. You might be unaware of them, but Coldfusion will automatically include such an application file when you open the helloworld page. I wont be surprised if that is the cause of the problem.
3. Is there an easier way to get the output from ColdFusion in MS-Word format than what I am doing?
Since the usual place to run code is the web root, C:\ColdFusion8\wwwroot, begin by creating a directory there. Something like
C:\ColdFusion8\wwwroot\word_from_CF
Copy the file helloworld.cfm into the new directory. I would also modify the helloworld code as follows:
(a) Connections are out of your hands. They may spawn threads that are notoriously difficult to synchronize. So, I would get rid of the connection niceties, and go for the kill by creating the object directly;
(b) Customize the path of the new doc file;
(c) Add a message at the end to let you know when you're done.
<!--- Create to the Word application object --->
<CFOBJECT
ACTION="CREATE"
CLASS="Word.Application"
NAME="objWord"
TYPE="COM">
<CFSCRIPT>
/* This will open Word if running locally */
objWord.Visible = true;
/* This returns the 'Documents' collection the Word Object */
objDoc = objWord.Documents;
/* Create a new document */
newDoc = objDoc.Add();
/* file name containing time stamp, to make it unique */
new_filename = "mydoc" & timeformat(now(),"HH_MM_SS") & ".doc";
/* make path of new file to point to same directory as helloword.cfm */
filepath = getdirectoryfrompath(expandpath('*.*')) & new_filename;
/* Save the document to the location */
newDoc.SaveAs(filepath);
/* We specify the range of '0' -- start at the beginning of the document */
docRange = newDoc.Range(0);
/* Add text to the range */
docRange.Text = "Hello World again and again!";
/* Save the changes */
newDoc.Save();
/* Close the document */
newDoc.Close();
/* Quit Word */
objWord.Quit();
</CFSCRIPT>
Done creating word file, <cfoutput>#filepath#</cfoutput>
Copy link to clipboard
Copied
You might consider using a Java component to create your Word documents. ColdFusion runs on top of a JVM and allows use of Java objects in CFML code.
"Using Java objects"
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Java_7.html#1177942
Aspose.Words for Java (commercial Java component)
http://www.aspose.com/categories/java-components/aspose.words-for-java/default.aspx
Apache POI (open source Java component)
http://poi.apache.org/hwpf/index.html
Copy link to clipboard
Copied
Is there anything you need to do with the setup of Word or ColdFusion to have them interact with eachother?
Under: Control Panel > Administrative Tools > Services > ColdFusion 8 App Server
In the "Log On" tab do you need to check the "Allow service to interact with desktop?
My code used to work with CF7 and Word 2003 but its not working now with CF8 and Word 2007 on a different computer. I'm getting the error "The SaveAs method was not found." when the checkbox above is UNchecked and when I check it I get an alert that switches me to the System desktop and shows that its getting hung up at the Save As dialogue box in Word open.
Any ideas?
Thanks!
Copy link to clipboard
Copied
If you are using the Word Com object, you are suffering the problems associated with that method.
MS Word is not a server based tool. Microsoft will tell you it is not a server based tool.
You can interact with the Com Object, but Microsoft makes no guarantee that the same methods will work the same from version to version.
And since it is not a server based tool, whenever any error is experienced the word process is going to send a message to the desktop and with for a human user to interact with the dialog box. Until that happens, any future attempts to communicate with the Word process by the Com object will fail.
There are generally easier ways to create documents that are meant to be opened with a Word application. But it depends on what you are actually attempting to accomplish.
Copy link to clipboard
Copied
The only thing I need to do is take a ".doc" file created using cffile and make it a "real" Word document.
I developed a Word file with my formatting and track changes enabled and saved it as an html file from Word. I then developed my CF code to take the code generated in the html file and replace the dynamic values. Next, I take the html with my values and use cffile to write a ".doc" file.
The problem is the ".doc" file needs to be opened and editable, thats fine but when you go to "Save As" in Word it tries to save as a ".htm" file. This doesn't seem like a big deal I know but the project obviously has requirements.
I will be on a server with IIS and CF8. Do you know any good options to bypass Word to make it a real Word document without spending anything or something with a smaller price tag?
Thanks for getting back to me so quick!
Copy link to clipboard
Copied
Just so you understand.
You are not creating a 'real' word document. You are creating an HTML document, that word just hapens to be able to read. You are then changing the extension of that file to be '.doc' so that word will automatically open said HTML document.
A real word document is a binary data format that you would need to use some program to create. Such as Word itself through the com object or the previously mentioned java based tools.
But to resolve your problem with the save as method naming the file with a .htm extension rather then the desired .doc extension. That should be able to be solved by using the proper HTTP header in the file to tell the browser what to name the file. Without this header defined, the HTTP request defaults to the name of the file requested.
<CFHEADER NAME="Content-disposition" value="attachment; filename=myWordFile.doc">
<CFCONTENT TYPE="application/msword" reset="true"...>
<!--- your html to be sent as a doc file --->
Copy link to clipboard
Copied
I understand I'm not creating a "real" word document. But when you open up the word document and use the Save As with a Val(0) it will become a REAL word document.
The problem I have is these files aren't downloaded, they are put into a zip file and used at a later date opened directly from Word and then saved again as a different file name. Its when they are saved as a different file name it wants to use .htm instead of .doc.
Thanks.
Copy link to clipboard
Copied
Then it sounds like you are stuck with trying to figure out what changed in the Word COM object between the 2003 version and 2007.
Or you could try the Java tools to create Word documents. The POI tools seems to be in your price range.
http://poi.apache.org/
Here's a few blogs that Google turned up for using POI with ColdFusion
http://www.bennadel.com/blog/474-ColdFusion-Component-Wrapper-For-POI-To-Read-And-Write-Excel-Files.htm
http://www.coldfusionjedi.com/index.cfm/2009/2/5/Reading-Office-documents-with-ColdFusion-2
http://cfsearching.blogspot.com/2008/08/how-to-install-poi-on-coldfusion-8.html
Copy link to clipboard
Copied
Thanks for the help!
That's what I'm trying to do with very little success. I'll check out those other things you mentioned.
The wierd thing is
wordDoc.SaveAs("c:\file.doc", Val(1));
works but it saves it as a ".dot" file. Same thing for 2, 3, 4 ,5.... just saves as different types txt, rtf, ...
The Val(0) which should be the Word file isn't.
wordDoc.SaveAs("c:\file.doc", Val(0));
Anyway, its got to be a setting or something where I can bypass a dialouge box or something.
Thanks again.
Copy link to clipboard
Copied
dbldutch wrote:
Anyway, its got to be a setting or something where I can bypass a dialouge box or something.
Only if the developers of the Word Com object API care that such a setting exists.
And since, as far as I know, MicroSoft does not care that their Word tool is used in a server setting as a back end process, there is a good chance that such a setting does not exist.
Now what they are considering with their future cloud based versions is anybodies guess.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now