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

Free Support Forum - aspose.com

Remove merge fields from output documents

Remove merge fields from output documents

Do we have a method in Aspose.Words that will allow us to remove all code fields from a word document? We want to remove merge field codes from several part of the word document not only at the end of the document. We are using Aspose.Words 4.4.2.0

We currenly use the Update/Unlink methods of the Document object using the Word PIA 2002.

Aspose.Words (?)

Word Iterop 2002

Unlink Method

Field object: Replaces the specified field with its most recent result.

Fields object: Replaces all the fields in the Fields collection with their most recent results.

expression.Unlink

expression Required. An expression that returns a Field or Fields object.

Remarks

When you unlink a field, it’s current result is converted to text or a graphic and can no longer be updated automatically. Note that some fields — such as XE (Index Entry) fields and SEQ (Sequence) fields — cannot be unlinked.

Example

This example unlinks the first field in “Sales.doc.”

Documents("Sales.doc").Fields(1).Unlink

This example updates and unlinks all the fields in the first section in the active document.

With ActiveDocument.Sections(1).Range.Fields
.Update
.Unlink
End With

Pasted from <http://msdn.microsoft.com/en-us/library/aa191947(office.10).aspx>

Hi

Thanks for your inquiry. You can use MailMerge.Delete Fields.

doc.MailMerge.DeleteFields();

Please see the following link for more information:

https://apireference.aspose.com/words/net/aspose.words.mailmerging/mailmerge/methods/deletefields

Hope this helps.

Best regards.

Hello there!

I’ve tried the code shown bellow. It does not work. The “” will only delete merge fields found at the end of the document. Our documents have merge fields and formula fields all over the place. This is what I tried before.

private void button1\_Click(object sender, EventArgs e)
{
    //POC remove merge fields
    //Open document which contains merge fields.
    Document doc = new Document(@"C:\Documents and Settings\pedrore\Desktop\BK-Dos\MyTesting.doc");
    //This is useful if you want to delete any merge fields that might be left at the end of the document.
    doc.MailMerge.DeleteFields();
    doc.Save(@"C:\Documents and Settings\pedrore\Desktop\BK-Dos\MyTesting02..doc");
}

Any ideas?

Thanks

Hi

Thank you for additional information. Would you please attach your document for testing? I will investigate this problem and provide you more information.

Best regards.

Here is a sample document which includes code fields. If you open the document using MS Word you can select the entire document, right click on the document, and then select “Toggle Field Codes” in order to see the field codes. We can programmatically use MS Word interop to remove all field codes by using the code snippet bellow:

With ActiveDocument.Sections(1).Range.Fields
.Update
.Unlink
End With

Thank you for your help

Pedro

Hi

Thanks for additional information. I thought that you would like to remove only merge fields from the document. If you need to remove all fields from the document you should remove all content between FieldStart and FieldEnd nodes. For example see the following code:

//Open document
Document doc = new Document(@"Test007\RemoveFields.doc");
//Get collection of FieldStarts from the document
NodeCollection starts = doc.FirstSection.Body.GetChildNodes(NodeType.FieldStart, true);
NodeCollection ends = doc.FirstSection.Body.GetChildNodes(NodeType.FieldEnd, true);
//Loop through all field starts and remove content between field start and end 
foreach (FieldStart start in starts)
{
    Node currentnode = start.NextSibling;
    //Remove content 
    while (currentnode.NodeType != NodeType.FieldEnd || currentnode.NodeType != NodeType.FieldStart)
    {
        currentnode = currentnode.NextSibling;
        if (currentnode != null)
            currentnode.PreviousSibling.Remove();
        else
            break;
    }
}

//Remove field starts and ends
starts.Clear();
ends.Clear();

//Save result document
doc.Save(@"Test007\out.doc");

The latest Aspose.Words API also has a build-in method for unlinking field, please see Document.UnlinkFields().

Best regards.