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

Free Support Forum - aspose.com

How to replace autodate in a word file using aspose.words

How to replace autodate in a word file to [AutoDate] text using aspose.words?

Hi

Thanks for your inquiry. Could you please attach sample document for testing. Input document and expected output document created manually. I will investigate the issue and try to help you.

Best regards.

Hi,

Please find attached the word document.

In the given document there are two places autodate are present. First in the header part and second in the body part.

I want to replace this autodate to a text value [AutoDate]

Hi

Thank you for additional information. It seems you meant DATE and TIME fields. I think the following code could help you.

//Open document and create DocumentBuilder

Document doc = new Document(@"Test082\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Get collection of FieldStartNode from document

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);

ArrayList dateStarts = new ArrayList();

//Loop through FieldStarts

foreach (FieldStart start in starts)

{

if (start.FieldType == FieldType.FieldDate || start.FieldType == FieldType.FieldTime)

{

dateStarts.Add(start);

//Move cursor to the field start position

builder.MoveTo(start);

//Insert [AutoDate] text

builder.Write("[AutoDate]");

}

}

//Now we should remove Date and Time fields

foreach (FieldStart start in dateStarts)

{

Node currentNode = start;

while (currentNode.NodeType != NodeType.FieldEnd)

{

currentNode = currentNode.NextSibling;

currentNode.PreviousSibling.Remove();

}

currentNode.Remove();

}

//Save output document

doc.Save(@"Test082\out.doc");

Best regards.