Connecting Tech Pros Worldwide Forums | Help | Site Map

Word, Access and Java - suggestions

Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#1: Jun 21 '09
Hi all,

I would lke some advice/suggestions on the following:

I have my data inputted into jtextbox's which is then assigned to String variables which is then inputted into a access database. (A weeks work!)
now, I need to use that data somehow to fill in fields for a invoice template in word.

any suggestions on where to start would great - I haven't a clue yet!

Regards

Brendan

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 21 '09

re: Word, Access and Java - suggestions


For starters, read this. It's Apache's POI project that allows Java to access a lot of MicroSoft's file formats (including MicroSoft Word).

kind regards,

Jos
Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#3: Jun 21 '09

re: Word, Access and Java - suggestions


Thanks for that Jos,

I decided to go with the wordApi. Although I can't get that working. ever used it? If so do you know where the wordapi.exe file goes as my program can't find it to execute the following..

Expand|Select|Wrap|Line Numbers
  1. WordProcessing.createNewDocumentFromTemplate("SampleTemplate.doc" );
  2. WordProcessing.typeTextAtBookmark("AddressLine1", "O'Reilly & Associated, Inc.");
  3. WordProcessing.typeTextAtBookmark("AddressLine2", "Mr Miller");
  4. WordProcessing.typeTextAtBookmark("AddressLine3", "101 Moris Street");
  5. WordProcessing.typeTextAtBookmark("AddressLine4", "Sebastopol, CA 95472-9902");
  6. WordProcessing.typeTextAtBookmark("Salutation", "Dear Mr Miller,");
  7. WordProcessing.exec();
going off the stack trace the exception being thrown happens when the following code is executed...

Expand|Select|Wrap|Line Numbers
  1. public static boolean exec() {
  2.     closeWordInput();
  3.     String cmd;
  4.     cmd = "wordAPI.exe";
  5.     try {
  6.       Runtime.getRuntime().exec(cmd).waitFor();
  7.     }
  8.     catch (Exception e) {
  9.       e.printStackTrace();
  10.       System.out.println(cmd + " could not be executed.");
  11.       System.out.println("Please ensure that WordAPI.exe may be found by java.exe by putting it in an appropriate directory.");
  12.       return false;
  13.     }
See anything anybody??
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 21 '09

re: Word, Access and Java - suggestions


A few questions for you: where does that executable file come from and what is it supposed to do? I posted my reply just 30 minutes ago; I don't believe that you've read the documentation that comes with POI-HWPF stuff after downloading and installing it. Please first read and then code; doing it the other way around (or just doing the second) doesn't make sense because it's silly.

kind regards,

Jos

edit: now I see what you have done: you've copied and pasted an example mentioned in Val's blog without any further Java knowledge; read up on the Runtime class and understand how the example works; you only did the second without doing the first and that is just silly.
Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#5: Jun 21 '09

re: Word, Access and Java - suggestions


Hi Jos,

I haven't read the poi documentation because I'm not using it as I said in my last post??

There isn't any installation documentation with what i am using but it seems so straight forward. The method documentation for what i am using can be read when my problem is solved, as it's useless to me until then.

Please believe me when i say the following - I have searched for the solution to my problem for about an hour without success. I wouldn't just ask before I've looked as - as you say it's silly.

should the following code execute if the file is there or is there an error in it?

Expand|Select|Wrap|Line Numbers
  1. cmd = "C:/Documents and Settings/Admin/My Documents/NetBeansProjects/JavaApplication1/src/javaapplication1/wordAPI.exe";
  2.  
my program is saying it can't find the file but it's there??
Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#6: Jun 21 '09

re: Word, Access and Java - suggestions


I've copied it Jos because the word template document included in the download is made for that code - I will write my own as soon as I get it working!!

I'm not silly - honest!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Jun 21 '09

re: Word, Access and Java - suggestions


Quote:

Originally Posted by brendanmcdonagh View Post

I've copied it Jos because the word template document included in the download is made for that code - I will write my own as soon as I get it working!!

I'm not silly - honest!

I believe you but you have to ask yourself a question: how is the exec() method supposed to find an arbitrary wordapi.exe file? Doesn't it have to be known somewhere in the PATH of the process (note: not the CLASSPATH). The easiest way is to put the executable file in the current working directory but then you have to find out what that directory is; there is no magic involved so you can't rely on any magic.

kind regards,

Jos
Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#8: Jun 21 '09

re: Word, Access and Java - suggestions


when you say path do you mean...

Expand|Select|Wrap|Line Numbers
  1.   String cmd;
  2.     cmd = "C:/Documents and Settings/Admin/My Documents/NetBeansProjects/JavaApplication1/src/javaapplication1/wordAPI.exe";
  3.     try {
  4.       Runtime.getRuntime().exec(cmd).waitFor();
same error - can't find file but it's there
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Jun 21 '09

re: Word, Access and Java - suggestions


Quote:

Originally Posted by brendanmcdonagh View Post

when you say path do you mean...

Expand|Select|Wrap|Line Numbers
  1.   String cmd;
  2.     cmd = "C:/Documents and Settings/Admin/My Documents/NetBeansProjects/JavaApplication1/src/javaapplication1/wordAPI.exe";
  3.     try {
  4.       Runtime.getRuntime().exec(cmd).waitFor();
same error - can't find file but it's there

There are spaces in that single String cmd and Java chops up that String to get the command itself and its arguments (and of course it does it not the way you intended (no magic, remember?)); there are also overloaded exec( ... ) methods that circumvent that inconvenience.

kind regards,

Jos
Reply