aspose file tools*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
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: 181
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: 181
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: 42635
    
  65
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


Ping & DNS - my free Android networking tools app
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: 61657
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
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://aspose.com/file-tools
 
subject: HWPF - Errors when inserting text into a word document