We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find and replace all in aspose

I have these lines of code to find and replace all in a doc using interop dll:

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

object findText = "";

object replaceText = "^&";

Microsoft.Office.Interop.Word.Find wf = doc.Content.Find;

wf.ClearFormatting();

wf.Font.Italic = 1;

wf.Replacement.ClearFormatting();

wf.Replacement.Font.Bold = 0;

wf.Text = "*";

wf.Execute(ref findText, ref noData, ref noData, ref noData, ref noData, ref noData, ref noData, ref noData, ref vTrue, ref replaceText, ref replaceAll, ref noData, ref noData, ref noData, ref noData);

Is this the replacement of the above code using aspose:

string findText = "";

string replaceText = "^&";

doc.Range.Replace(findText, replaceText, false, true);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Font.ClearFormatting();

builder.Font.Italic = true;

builder.Font.Bold = false;

builder.Write("*");

why two clearformating() method in the intrrop code ? I guess both are having the same functioanlity.

Thanks in advance.

Hi Komal,


Thanks for sharing the detail. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
http://www.aspose.com/docs/display/wordsnet/Object+Model+Overview
http://www.aspose.com/docs/display/wordsnet/Document+Tree+Navigation


The following code snippet replace the text only with current formatting. If you want to format the replaced contents, please implement the IReplacingCallback and check the code example shared at this link.

Document doc = new Document(MyDir + “Document.doc”);
doc.Range.Replace(“sad”, “bad”, false, true);

If you face any problem, please manually create your input and expected Word document using Microsoft Word and attach it here for our reference.  We will then provide you more information about your query along with code.

Hi Tahir,

The solution you have provide is only for string without having special character.

i.e

string findtext = "abc";

string replaceText = "^p";

doc.Range.Replace("abc", "^p", false, true); will not work.

So we need to put the special char string in a regex and do the replace as :

Regex regex = new Regex(Regex.Escape("abc"));

doc.Range.Replace(regex, "^p");

Now the issue is how to replace the ""(i.e null) with "^p" i.e.

Regex regex = new Regex(Regex.Escape(""));

doc.Range.Replace(regex, "^p");

The above code is throwing exception "can not replace null"

Please advise how can we replace null with the special char string.

Here i m mentioning the interop code . please suggest the alternative in aspose.

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

object findContinue = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

object findText = "";

object replaceText = "^&";

object inline = "InlineCode";

Microsoft.Office.Interop.Word.Find wf = doc.Content.Find;

wf.ClearFormatting();

wf.Replacement.ClearFormatting();

wf.Text = "*";

wf.Execute(ref findText,ref vFalse, ref noData, ref vTrue, ref noData, ref noData, ref vTrue, ref findContinue,ref vTrue, ref replaceText, ref replaceAll, ref noData, ref noData, ref noData, ref noData);


Please assist…

Hi Komal,


Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v13.5.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v13.5.0 and let us know how it goes on your side. I hope, this will help.

If the problem still remains, please attach your input and expected output Word document here for testing. I will investigate the issue on my side and provide you more information along with code.

Moreover, if you are replacing the text with html please check following code example for your kind reference. This example replaces text specified with regular expression with HTML.


<span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:blue;mso-fareast-language:EN-GB”>public<span style=“font-size:
10.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
mso-fareast-language:EN-GB”> void
ReplaceWithInsertHtml()<o:p></o:p>

{

// Open the document.

Document doc = new Document(MyDir + "Range.ReplaceWithInsertHtml.doc");

doc.Range.Replace(new Regex(@""), new ReplaceWithHtmlEvaluator(), false);

// Save the modified document.

doc.Save(MyDir + "Range.ReplaceWithInsertHtml Out.doc");

}

private class ReplaceWithHtmlEvaluator : IReplacingCallback

{

///

/// NOTE: This is a simplistic method that will only work well when the match

/// starts at the beginning of a run.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

builder.MoveTo(e.MatchNode);

// Replace '' text with a red bold name.

builder.InsertHtml("James Bond");

e.Replacement = "";

return ReplaceAction.Replace;

}

}

The method using interop is

private void italicToHTML(Microsoft.Office.Interop.Word.Document doc)

{

//AppLog.trace("italicToHTML");

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

object findText = "";

object replaceText = "^&";

Microsoft.Office.Interop.Word.Find wf = doc.Content.Find;

wf.ClearFormatting();

wf.Font.Italic = 1;

wf.Replacement.ClearFormatting();

wf.Replacement.Font.Bold = 0;

wf.Text = "*";

wf.Execute(ref findText, ref noData, ref noData, ref noData, ref noData, ref noData, ref noData, ref noData, ref vTrue, ref replaceText, ref replaceAll, ref noData, ref noData, ref noData, ref noData);

}

I/P: Hello there i am here for you

O/p: Hello there i am here for you

Need the aspose code for the above method.

Hi Komal,


Thanks for sharing the detail. Run Class represents a run of characters with the same font formatting. All text of the document is stored in runs of text.

Please read ‘Find and Replace Overview’ and use the Run.Font.Italic property to check either a Run node has italic formatting or not and add italic tag according to your requirements with Run.Text. Hope this helps you.

if (run.Font.Italic)<o:p></o:p>

run.Text = "" + run.Text + "";


If you face any issue, please share your input Word document here. We will then provide you more information on this along with code.

Hi Tahir,

Can you please make me understand what the below interop code is meant for: and what should be the ASPOSE replacement.

private void programFontToHTML(Document doc)

{

AppLog.trace("programFontToHTML");

object replaceAll = WdReplace.wdReplaceAll;

object findContinue = WdFindWrap.wdFindContinue;

object findText = "";

object replaceText = "^&";

object inline = "InlineCode";

Find wf = doc.Content.Find;

wf.ClearFormatting();

wf.Replacement.ClearFormatting();

wf.Text = "*";

wf.Execute(ref findText, ref vFalse, ref noData, ref vTrue, ref noData, ref noData, ref vTrue, ref findContinue, ref vTrue, ref replaceText, ref replaceAll, ref noData, ref noData, ref noData, ref noData);

}

Could you please assist me on this.

Hi Komal,


Thanks for your inquiry. You have used the empty string for findText, it will search for formatting only. Please note that Aspose.Words does not work in the same way as Word interop do for ‘Find and Replace’. I suggest you, please read ‘Find and Replace Overview’.

It would be great if you please share your input Word document and expected output Word document here for our reference. We will investigate, how you want your final Word output be generated like. We will then provide you more information on this along with code.