
Index: scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
===================================================================
--- scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java	(revision 760867)
+++ scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java	(working copy)
@@ -739,7 +739,7 @@
     subRange.insertBefore(pValue);
 
 	if (subRange.getEndOffset() != previousEndOffset)
-		_end += (subRange.getEndOffset() - previousEndOffset);
+		adjustForInsert(subRange.getEndOffset() - previousEndOffset);
 
     // re-create the sub-range so we can delete it
     subRange = new Range(
Index: scratchpad/testcases/org/apache/poi/hwpf/data/Bug45269.doc
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

Property changes on: scratchpad\testcases\org\apache\poi\hwpf\data\Bug45269.doc
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Index: scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java
===================================================================
--- scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java	(revision 760867)
+++ scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java	(working copy)
@@ -171,4 +171,37 @@
 		HWPFDocument doc2 = writeOutAndRead(doc);
 		assertEquals("Nick Burch", doc.getSummaryInformation().getAuthor());
 	}
+	
+	/**
+	 * Test for reading paragraphs from Range after replacing some 
+	 * text in this Range.
+	 * Bug #45269
+	 */
+	public void testReadParagraphsAfterReplaceText()throws Exception{
+  	HWPFDocument doc = new HWPFDocument(new FileInputStream(
+  			new File(dirname, "Bug45269.doc")));
+    Range range = doc.getRange();
+    
+    //check replace with longer text
+    for (int x = 0; x < range.numParagraphs(); x++) {
+      Paragraph para = range.getParagraph(x);
+      int offset = para.text().indexOf("campo1");
+      if (offset >= 0) {
+          para.replaceText("campo1", " foi porraaaaa ", offset);
+      }
+    }
+    
+  	doc = new HWPFDocument(new FileInputStream(
+  			new File(dirname, "Bug45269.doc")));
+    range = doc.getRange();
+    
+    //check replace with shorter text
+    for (int x = 0; x < range.numParagraphs(); x++) {
+      Paragraph para = range.getParagraph(x);
+      int offset = para.text().indexOf("campo1");
+      if (offset >= 0) {
+          para.replaceText("campo1", " foi ", offset);
+      }
+    }
+	}
 }
Index: scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
===================================================================
--- scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java	(revision 760867)
+++ scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java	(working copy)
@@ -113,7 +113,7 @@
 		assertEquals(1, range.numSections());
 		section = range.getSection(0);
 
-		assertEquals(4, section.numParagraphs());
+		assertEquals(5, section.numParagraphs());
 		para = section.getParagraph(2);
 
 		text = para.text();
