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

Free Support Forum - aspose.com

Delete sections with remaining mergefields

I am new to Aspose and I am trying to delete the sections that are containing any remaining mergefields.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I am struggling with this for a while now.

Any help is really appreciated.

I am stuck at:

Aspose.Words.Document document = new Document(this._DocumentStream);

DocumentBuilder builder = new DocumentBuilder(document);

// a list with the remaining merge fields

List<string> emptyFields = document.MailMerge.GetFieldNames().ToList<string>();

while (emptyFields.Count != 0)

{

string mergeField = emptyFields[0];

mergeField = emptyFields[0];

builder.MoveToMergeField(mergeField);

Section section = builder.CurrentSection;

if (section.ParentNode != null)

{

document.Sections.Remove(section);

}

}

document.Save(this._DocumentStream, this._DocumentFormat);

Thanks in advance.

Hello

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

Thanks for your inquiry. Please try using the following code:

Document document = new Document("in.doc");

DocumentBuilder builder = new DocumentBuilder(document);

// Get all filds names

string[] fldNames = document.MailMerge.GetFieldNames();

foreach (string fieldName in fldNames)

{

builder.MoveToMergeField(fieldName);

// Get current section

Section section = builder.CurrentSection;

if (section.ParentNode != null)

{

document.Sections.Remove(section);

}

}

document.Save("out.doc");

Best regards,

Thanks for the fast reply.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Too bad it didn’t solve my problem. (The output stays the same as with my code)

But I figured out what is going wrong:

It is always deleting headers and footers.

(Even if there are no remaining mergefields in these sections.)

On the other hand it is also doing what I want it to do by deleting the sections with remaining mergefields.

Is there a way that my headers and footers will not be deleted?

Thanks in advance!

Hello

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

Thanks for your inquiry. Could you please attach your input document here for testing? I will check the problem on my side and provide you more information.

Best regards,

I found the solution to this problem!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Here is the code:

// Get all filds names

string[] fldNames = document.MailMerge.GetFieldNames();

foreach (string fieldName in fldNames)

{

builder.MoveToMergeField(fieldName);

if (builder.CurrentSection != null)

{

Body body = builder.CurrentSection.Body;

if (body != null && body.ParentNode != null)

{

if (builder.CurrentSection.HeadersFooters.Count > 0)

{

body.Remove();

}

else if (body.ParentSection.ParentNode != null)

{

body.ParentSection.Remove();

}

}

}

}

Thanks anyway!

Hello

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

It is perfect, that you already resolved the problem.

Best regards,