Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Word
I want to replace text parts of Sentences. my approach is
Iterate on Sentences , since I use like this Document.Sentences.Count;
then I replace then in two way.
if Sentences is only includes text, I replace whole Sentences as below
Range rng = Sentences[i].Range(Start,End);
rng.Text = str ;
rng.Select;
 
if Sentences includes pictures, OLE object and ActiveX so on.
I extract the pure text part and apply above code to replace it.and the start and end position of text part is correct, I checked it.
 
problem is that when I replacing parts of Sentences it throw "Cannot delete Range " exception.
how can I do this if I want replace text part of Sentences which includes both text and picture?
 
thanks!
Posted 23-Mar-11 0:19am
Comments
Apfelmuuus at 24-Mar-11 6:01am
   
I can't figure out what is the problem because I do not know how you want to replace "parts of Sentences"
 
One possible problem can be found at msdn:
 
"Note Range objects are independent of the selection. That is, you can define and manipulate a range without changing the selection.[...]"MSDN

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I recommend you use Spire.Doc http://www.e-iceblue.com/Introduce/word-for-net-introduce.html[^]
Without MS Office installing.
Here is a sample code:
 
//Create word document
Document document = new Document();
 
//load a document
document.LoadFromFile(@"..\..\Data\FindAndReplace.doc");
 
//Replace text
document.Replace("word", "excel",true,true);
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 8 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100