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

Free Support Forum - aspose.com

Replacing tags

When trying to Replace tags in Word Document it fails for particular tag, Can you help me on this. Here i have added Sample code for your reference.

Input:

string documentPath = "\\\\F:\\Test";
string documentName = "Test.docx";

string Texttxt = @"---&&&<<<Some Text Here>>>[";
string textbottom = @"]&&&---";
Document document = new Document(documentPath + documentName);
//  Document destinationDoc = document;

document.Range.Replace(Texttxt, "");
document.Range.Replace(textbottom, "");
document.Save(documentPath + documentName);

Output:
Evaluation Only. Created with Aspose.Words. Copyright 2003-2022 Aspose Pty Ltd.

—&&&<<>>

&

@NanthiniSenthil123 Could you please also attach your input and expected output documents here for our reference? We will check the issue and provide you more information.

string documentPath = "\\\\F:\\Test";
string documentName = "Test.docx";

string Texttxt = @"---&&&<<<Some Text Here>>>[";
string textbottom = @"]&&&---";
Document document = new Document(documentPath + documentName);
//  Document destinationDoc = document;

document.Range.Replace(Texttxt, "");
document.Range.Replace(textbottom, "");
document.Save(documentPath + documentName);

Expected Output: expected Output is to Remove tags with empty space

@NanthiniSenthil123 & character is used by Aspose.Words for escaping special symbols, like paragraph break or section break. See remarks section in Range.Replace method description. So you should either use legacy mode of replace method:

string Texttxt = @"---&&&<<<Some Text Here>>>[";
string textbottom = @"]&&&---";
Document document = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.LegacyMode = true;
document.Range.Replace(Texttxt, "", opt);
document.Range.Replace(textbottom, "", opt);
document.Save(@"C:\Temp\out.docx");

Or use additional & for escaping:

string Texttxt = @"---&&&&&&<<<Some Text Here>>>[";
string textbottom = @"]&&&&&&---";
Document document = new Document(@"C:\Temp\in.docx");
document.Range.Replace(Texttxt, "");
document.Range.Replace(textbottom, "");
document.Save(@"C:\Temp\out.docx");

Test document is attached: in.docx (12.0 KB)
out.docx (9.4 KB)