PDA

View Full Version : how to open ms word ?


dikdikdik
12-22-2008, 08:48 AM
Hi everybody. I am a newbie in Actionscript language and have a project that relation with Ms word. I want to build an AIR desktop application which can open a Ms word, edit and save as plain text.

I try to search about the issue, and find out some reference code for text editor

private function fileReadHandler(event:Event):void
{
var str:String = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g");
str = str.replace(lineEndPattern, "\n");
mainTextField.text = str;
stream.close();
}

but when I correct code to use for word document, it is error. Anyone can help me to solve the problem?

In addtion, I have new idea about microsoft win32OLE. But I don't know how to use it in actionscript.

Thank any help!!!

evride
12-23-2008, 05:24 AM
first, use the tags around text to format it nicely.
second give me the whole error - number and text.

also, you have to read the byte data into a bytearray and not straight into a string.

[as]
var ba:ByteArray = stream.readUTFBytes(stream.bytesAvailable);
stream.close();

karthickraj_knowledgeq
12-23-2008, 09:38 AM
you try fscommand()?..(This only for open a ms word and i believe it will enough for you)
Regards
Karthick Raj.S

evride
12-23-2008, 06:00 PM
you cant do use fscommand in air.
and what i think he's trying to do is make an app that does the same thing as word (edit .doc/.docx files) and doesnt want to start the program up

corbo950
12-23-2008, 11:02 PM
You might want to try using Microsoft's SDK for opening word files cause I know that is like what Open Office and the iPhone use to read them. Would also let you save back to .doc/.docx files if you were so inclined

dikdikdik
12-24-2008, 08:26 AM
Hi evdog, thank your answer but your code don't run.


var ba:ByteArray = stream.readUTFBytes(stream.bytesAvailable);
stream.close();


The error is
1067: Implicit coercion of a value of type uint to an unrelated type flash.utils:ByteArray.

I use "text editor" code in adobe website to test with text file and MS document file. It can't read characters in MS document. I think the reason is format of Ms document is different with text file.

My AIR application can be explain that : firstly I open ms document file, the air app try to read bytes or use extern API to read, then display in text area. Secondly I will parse data in text area to some paragraph and save to plain text file.

I have problem when read MS document, I try to use readUTFBytes, readBytes but not ok. I search and find another solution that use Actionscript to call extern API e.g ActiveX objects, win32OLE, Microsoft Office Web Components... to read ms word and convert it to text.
My idea is
1. Read ms word by any program
2. convert it to plain text and save as temporary file
3. display it to text area

May be MS word file can contain tables, so I want to use VBA to convert tables to text after step1 and before step 2.

Thank any suggest

Hai Anh

runawayprisoner
12-25-2008, 04:56 PM
Hi evdog, thank your answer but your code don't run.


var ba:ByteArray = stream.readUTFBytes(stream.bytesAvailable);
stream.close();


The error is
1067: Implicit coercion of a value of type uint to an unrelated type flash.utils:ByteArray.

I use "text editor" code in adobe website to test with text file and MS document file. It can't read characters in MS document. I think the reason is format of Ms document is different with text file.

My AIR application can be explain that : firstly I open ms document file, the air app try to read bytes or use extern API to read, then display in text area. Secondly I will parse data in text area to some paragraph and save to plain text file.

I have problem when read MS document, I try to use readUTFBytes, readBytes but not ok. I search and find another solution that use Actionscript to call extern API e.g ActiveX objects, win32OLE, Microsoft Office Web Components... to read ms word and convert it to text.
My idea is
1. Read ms word by any program
2. convert it to plain text and save as temporary file
3. display it to text area

May be MS word file can contain tables, so I want to use VBA to convert tables to text after step1 and before step 2.

Thank any suggest

Hai Anh

Okay... apparently, what you are trying to do (make it globally accessible in all platforms?) is a bit... far-fetched at this level.

Anyhow, here's what I use to read all data from a file stream to a ByteArray, and please note that you should open the entire document synchronously and not asynchronously since looping back and forth would be quite a mess for a document.

With that out of the way, here's the code:


filestrm.readBytes(mem,0,file.size);


Legend:

mem is a ByteArray. It doesn't matter how big it is, but if you want to conserve memory, pre-define a valid limit. Though a limit may cap the program and somehow prevents it from opening really large documents.

filestrm is the FileStream that we obtained the file from using the 'open' method. (please no 'openAsync' here)

file is the File that we browsed for earlier.

And... there you go.

We can't use writeBytes because FileStream is not a ByteArray, though its behaviors are exactly like one, therefore... readBytes.

So, that's how you obtain the raw data of the word document. Next step is to interprete that binary data, and luckily, Microsoft provided documents.

http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Word97-2007BinaryFileFormat(doc)Specification.pdf

^= You can grab the binary specifications for Word 97 - 2007 from that link. It's officially Microsoft's and totally free.

There are just about 210 pages of that document to read. It seems tedious, but if you can understand by the end of the day, you may be able to build something on par with Microsoft Word or OpenOffice Writer.

Good luck.

dikdikdik
12-26-2008, 06:25 AM
Thank runawayprisoner, but if choice the way which decode raw data will loss many time and I'm afraid I don't finish it :(

I use AIR app mainly in Win OS, so I still find the way to relate Flex with another program that can open MS document file. In Ruby language I can use Win32 OLE from Ruby to call Microsoft Word which help me open and save document to plain text e.g.

require 'win32ole'
word = WIN32OLE.new('Word.Application')
document = word.Documents.Open('abc.doc', 'ReadOnly' => true)
document.SaveAs('abc.txt')
document.Close

But in Actionscript, any one know about the way to use FScommand or ExternalInterface to call the same thing as above? Plz help me

dikdikdik
12-29-2008, 09:00 AM
I try to use Javascript inside Actionscript which call MS word to read document. But it error when I initial ActiveXObject. Here is my code

test.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
private var calledFromJSHandlerFunction:Function = calledFromJSHandler;
private function init():void{
html.addEventListener(Event.HTML_DOM_INITIALIZE, domInitialized);
html.location = "htmlwithJS.html";
}
private function domInitialized(event:Event):void{
html.htmlLoader.window.calledFromJSHandlerFunction = calledFromJSHandlerFunction;
}
private function calledFromJSHandler():void {
mx.controls.Alert.show("ActionScript called from JavaScript", "Alert");
}
]]>
</mx:Script>
<mx:HTML id="html" x="137" y="40" width="339"/>

</mx:WindowedApplication>

htmlwithJS.html

<html>
<script language="Javascript">
function loadworddoc(){
var doc = new ActiveXObject("Word.Application");
doc.Visible=false;
doc.Documents.Open("C:\Sam\Sam.doc");
var txt;
txt = doc.Documents("C:\Sam\Sam.doc").Content;
document.all.myarea.value = txt;
doc.quit(0);
}
</script>
<body>
<input type="button"
value="Load"
onclick="loadworddoc()">
<br />
<textarea name="myarea">test now</textarea>
</body>
</html>


The message is : ReferenceError: Can't find variable: ActiveXObject

Any one can give me some advice?

jewelthief
12-31-2008, 04:36 AM
Here is what i have done. I am not an expert but i found the one solution from which you can invoke any kind of executable. As clearly mentioned that you cannot use Fscommand in AIR so there has to be some intermediate solution.

What i have done is to make a Windows service (It runs in the background) in .net and in that service, I used FileSystemWatcher class.

My AIR app creates XML file on filesystem (say C:/) with the path of the executable to run and as soon as it creates XML, the windows service which is constantly watching the file System reads the path of the exe from newly created xml file(from AIR) and invokes that exe.

It worked for me. You can try it.

dikdikdik
12-31-2008, 08:06 AM
Here is what i have done. I am not an expert but i found the one solution from which you can invoke any kind of executable. As clearly mentioned that you cannot use Fscommand in AIR so there has to be some intermediate solution.

What i have done is to make a Windows service (It runs in the background) in .net and in that service, I used FileSystemWatcher class.

My AIR app creates XML file on filesystem (say C:/) with the path of the executable to run and as soon as it creates XML, the windows service which is constantly watching the file System reads the path of the exe from newly created xml file(from AIR) and invokes that exe.

It worked for me. You can try it.

Thank jewelthief !!! Can you help me more. I'm stuck with the problem :( because I am beginning to learn AIR and Actionscript. I don't understand your way. How do I use windows service to call an application e.g MS Word? Can I contact with you ? My mail is [email protected]

dikdikdik
01-05-2009, 02:53 AM
nobody can help me :( I'm stuck with it

dikdikdik
01-17-2009, 01:01 AM
Althought nobody help me but I find one solution can help me. Of cause, it is a great solution for anyone want to create the relation between AIR app and native app. This is link http://aperture.fluorinefx.com/