• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HWPF - Errors when inserting text into a word document

 
Bawa Nathan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bawa Nathan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bawa Nathan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Bee Sat", please check your private messages for an important administrative matter.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic