We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Request WParagraph.Replace

The Replace-method in the class WordDocument has an overload to replace a TextbodyPart.
The Replace-method in the class WParagraph doesn't have this overload.

On this moment I need this to replace a find-word by a replace-word but in another color, this in combination with a certain style.


5 Replies

BP Bhuvaneswari P Syncfusion Team February 26, 2009 12:28 PM UTC

Hi Aart,

Thank you for your interest in Syncfusion products.

In paragraph object you can find only string Replace method and this method not possible to have replacing the TextBodypart. Since TextBodyPart is collection of body items.

If you want to find and replace the specific word. Please use the below code snippet and if you want to set more than one character style then please create a new characterFormat style by using the WCharacterFormat and then define new styles and assign this format to the specified TextRange object as we have suggested in your Direct trac incident.

[VB]


Dim chara As WCharacterFormat = New WCharacterFormat(document)
chara.UnderlineStyle = UnderlineStyle.Dotted
chara.FontSize = 18.0F
chara.TextBackgroundColor = Color.Blue

Dim document As WordDocument = New WordDocument("..\..\Template.doc")
For Each ent As Entity In document.ChildEntities
If TypeOf ent Is WSection Then
Dim section As WSection = TryCast(ent, WSection)
For Each paragraph As WParagraph In section.Body.Paragraphs
For Each item As ParagraphItem In paragraph.Items
If item.EntityType = EntityType.TextRange Then
Dim range As WTextRange = TryCast(item, WTextRange)
If range.Text.Contains("DocIO") Then

range.ApplyCharacterFormat(chara)
End If
End If

Next item
Next paragraph
End If
Next ent


Online documentation link for Character format:
http://help.syncfusion.com/ug_71/docio/WCharacterFormat.html

Please let me know if you need any further assistance regarding this.

Best Regards,
Bhuvana




AV Aart van Assen February 26, 2009 02:27 PM UTC

Not completely.

For Each sect As WSection In document.Sections
For Each par As WParagraph In sect.Paragraphs
If par.GetStyle.Name.ToString.ToLower = sStyle.ToLower Then
Dim chara As WCharacterFormat = New WCharacterFormat(document)
chara.UnderlineStyle = UnderlineStyle.Single
chara.TextColor = Drawing.Color.Red

For Each item As ParagraphItem In par.Items
If item.EntityType = EntityType.TextRange Then
Dim trange As WTextRange = TryCast(item, WTextRange)
If trange.Text.Contains(txtSearch.Text) Then
' Replace the txtSearch.text by txtReplace.text

' And apply red-underline
trange.ApplyCharacterFormat(chara)
End If
End If
Next item
End If
Next
Next

This change the style of a completely line and not from the search item (what can be more than 1 word, but also 1 letter)

And I have to change the search-item by the replace-item. But this can be done by WParagraph.Replace, if this will not mess up the character-format that I just have done.



>Hi Aart,

Thank you for your interest in Syncfusion products.

In paragraph object you can find only string Replace method and this method not possible to have replacing the TextBodypart. Since TextBodyPart is collection of body items.

If you want to find and replace the specific word. Please use the below code snippet and if you want to set more than one character style then please create a new characterFormat style by using the WCharacterFormat and then define new styles and assign this format to the specified TextRange object as we have suggested in your Direct trac incident.

[VB]


Dim chara As WCharacterFormat = New WCharacterFormat(document)
chara.UnderlineStyle = UnderlineStyle.Dotted
chara.FontSize = 18.0F
chara.TextBackgroundColor = Color.Blue

Dim document As WordDocument = New WordDocument("..\..\Template.doc")
For Each ent As Entity In document.ChildEntities
If TypeOf ent Is WSection Then
Dim section As WSection = TryCast(ent, WSection)
For Each paragraph As WParagraph In section.Body.Paragraphs
For Each item As ParagraphItem In paragraph.Items
If item.EntityType = EntityType.TextRange Then
Dim range As WTextRange = TryCast(item, WTextRange)
If range.Text.Contains("DocIO") Then

range.ApplyCharacterFormat(chara)
End If
End If

Next item
Next paragraph
End If
Next ent


Online documentation link for Character format:
http://help.syncfusion.com/ug_71/docio/WCharacterFormat.html

Please let me know if you need any further assistance regarding this.

Best Regards,
Bhuvana






BP Bhuvaneswari P Syncfusion Team March 2, 2009 01:03 PM UTC

Hi Aart,

Thanks for the update.

Using Essential DocIO we can't access particular word or letter and format the word. We can format only each paragraph items by using the WTextRange class characterFormat property.

Unfortunately it is not possible to preserve both existing and new format while replacing. Either you can preserve the existing or new format. If you replace the format using selection(i).GetAsOneRange() method this will preserve only the new format and it will remove the old format.

Note: Sorry in the last post instead of updating find and format line which contains the find word, we have updated to find and replace the specific word.

Please let me know if you need any further clarification.

Best Regards,
Bhuvana



AV Aart van Assen March 3, 2009 10:22 AM UTC

Hello Bhuvana,
Sorry, but I don't understand the note in your last post.

Maybe it is possible to clarify what you are meaning.



BP Bhuvaneswari P Syncfusion Team March 4, 2009 09:35 AM UTC

Hi Aart,

In my first update I have updated wrongly that "If you want to find and replace the specific word" instead of updating find a word and replace the text range with formatting. So I just clarified in the last update note.

Please let me know if you need more clarification.

Best Regards,
Bhuvana



Loader.
Live Chat Icon For mobile
Up arrow icon