This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes HWPF - Errors when inserting text into a word document Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Reply Bookmark "HWPF - Errors when inserting text into a word document" Watch "HWPF - Errors when inserting text into a word document" New topic
Author

HWPF - Errors when inserting text into a word document

Bawa Nathan
Greenhorn

Joined: Nov 01, 2009
Posts: 7
Hello all

I'm extremely new to POI. I tried one of the examples to insert test into word but I received errors when attempting to run it.

Code:
HWPFDocument doc = new HWPFDocument(new FileInputStream("D:\\temp\\test2.doc"));
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!!! HAHAHAHAHA I DID IT!!!");
OutputStream out = new FileOutputStream("D:\\temp\\test2.doc");
doc.write(out);
out.flush();
out.close();

Error:
Exception in thread "main" java.lang.IllegalArgumentException: The end (43) must not be before the start (80)
at org.apache.poi.hwpf.usermodel.Range.sanityCheckStartEnd(Range.java:247)
at org.apache.poi.hwpf.usermodel.Range.<init>(Range.java:180)
at org.apache.poi.hwpf.usermodel.CharacterRun.<init>(CharacterRun.java:96)
at org.apache.poi.hwpf.usermodel.Range.getCharacterRun(Range.java:796)
at org.apache.poi.hwpf.usermodel.Range.insertAfter(Range.java:444)
at export.ExportToWord.main(ExportToWord.java:21)

Could anyone help please?

Thanks heaps!
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 180
Bee, welcome to Javaranch!

can you please use code tags so that your code becomes readable? Also don't forget to mention the line on which its throwing exception. ExportToWord.java:21 in this case.


love your job and not your company;
...because you never know when your company will stop loving you!
Bawa Nathan
Greenhorn

Joined: Nov 01, 2009
Posts: 7
Hi Manoj

Thanks for your response!

Oops, sorry about that! I didn't see those tab buttons on top as I typed, I just noticed it now! The error line 21 refers to :


Let me know if I need to provide more info on this to make sense.

Thanks once again!
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 180
Probably it has something to do with the way you have initialized Range ... just a guess. Until you post all the relevant code, its difficult to help you out.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
Are you by any chance doing something with tables? There's at least one bug that produces this message; there may be more.

Maybe this code helps: CreateWordDocument


Android appsImageJ pluginsJava web charts
Bawa Nathan
Greenhorn

Joined: Nov 01, 2009
Posts: 7
No, I'm not using tables. I tried the code you referred to, but I'm getting a NullPointerException for this line:



Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56200
    
  13

"Bee Sat", please check your private messages for an important administrative matter.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
As the comment says, the stuff about custom properties is just showing off extra functionality; you can remove it if you don't use them.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: HWPF - Errors when inserting text into a word document
 
Similar Threads
java.io.FileNotFoundException
How to create MS Word Doc using HWPF POI
Mearging two word documents using POI
Creating word documents file from templates.
Reading word Documents Using JAVA