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

Free Support Forum - aspose.com

Range.Replace method issues

Hi,

we are developping a program where we need to open a Word document, replace some predifined tags with some multiline text and finally save the document in pdf.

I'm currently evaluating Aspose.Word for .net for this purpose and I find some issues when trying to use the range.replace method.

My code could ideally be as simple as :

Dim doc As New Spire.Doc.Document(myWordFileName)
doc.Range.Replace(myTagText, myReplacementText, False, True)
doc.SaveToFile(myPDFFileName)

But when trying to run this code we find out that

- The multiline replacement text is not supported.
- The isMatchWholeWord parameters is inoperant.

I looked for a solution in this forum but I didn't find any. Could you tell me if these issues are going to be addressed in a future release (I found a post from 2005 telling that this would be corrected in a few months...) or do you have a workaround to achieve the same functionnalities with some sample code?

Thank you in advance

Aspose.Words for .NET

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please attach your document hare for testing and specify what tags you need to replace? I will check the issue and provide you more information.

1. Aspose.Words does not allow using special symbols in captured and replacement strings. That is why you cannot replace text that is spanned between paragraphs. If you need to replace your tag with multiline text, you can achieve this by using ‘\v’ (line break character) instead of ‘\n’ (paragraph break character).

2. isMatchWholeWord option works fine on my side. Please provide more information about the problem.

Best regards,

Hi again,

1) any clue on how to replace \n by \v in a text extracted from a regular multiline TextBox in vb.net?

2) I have attached three documents.

  • TemplateDoc.doc is the base template. The tag I want to replace is $001.
  • The document called "WithoutIsMatchWholeWord.doc" is generated with the following code

Dim doc As New Aspose.Words.Document(myFileName)
doc.Range.Replace("$001", "Barros", False, False)
doc.Save(myFileName)

As you can see, both $001 and $001B tag were replaced

  • The document called "WithIsMatchWholeWord.doc" is generated with the following code

Dim doc As New Aspose.Words.Document(myFileName)
doc.Range.Replace("$001", "Barros", False, True)
doc.Save(myFileName)

As you can see, none of the tags were replaced.

Thank you

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information.

1. Just use code like the following:

' Read some string

Dim s As String = File.ReadAllText("C:\Temp\test.txt")

' Replace paragraph breaks with line breaks.

s = s.Replace(ControlChar.CrLf, ControlChar.LineBreakChar)

s = s.Replace(ControlChar.Cr, ControlChar.LineBreakChar)

s = s.Replace(ControlChar.Lf, ControlChar.LineBreakChar)

2. $ character is not recognized as a part of word. This is correct behavior. If you open your document in MS Word, press Ctrl+F (find) and type something like this “$0001”, you will see that “Match whole Word” option is inactive.

Best regards.

Hi,

1. Thank you very much, it (almost) works. In fact the code should look like this:

' Read some string<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Dim s As String = File.ReadAllText("C:\Temp\test.txt")

' Replace paragraph breaks with line breaks.
s = s.Replace(ControlChars.CrLf, ControlChars.VerticalTab)
s = s.Replace(ControlChars.Cr, ControlChars.VerticalTab)
s = s.Replace(ControlChars.Cr, ControlChars.VerticalTab)

2. Ok, any idea why this is a correct behaviour?

Best regards

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry.

1. Yes, you variant of code will also work as expected. I just used Aspose.Words.ControlChar:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.controlchar.html

2. This is because this sign is a punctuation mark, so it cannot be a part of word, as coma and semicolon cannot be.

Best regards,