We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DocIO: Tables in Word-Document

Is it possible to hide or delete an existing table from Word document, if some condition is true (DataTable is empty for example)

DataTable mDataTable = GetTestOrder(ID);

if (mDataTable.Rows.Count == 0)
    REMOVE / DELETE / HIDE Table???
else
    doc.MailMerge.ExecuteGroup(mDataTable);

5 Replies

AK Ananda Kumar V Syncfusion Team February 28, 2013 01:36 PM UTC

Hi Silvester,

Thank you for your interest in Syncfusion products.

To remove an existing table from word document, you can simply call below code snippet.

Code snippet:

//Get Table object

IWTable table = document.Section[0].Tables[0];

//Remove table if DataTable is empty

if (mDataTable.Rows.Count == 0)

        //Remove Table object from the child entity collection of first section’s textbody

        document.Section[0].Body.ChildEntities.Remove(table);

//Alternatively you can directly remove the table from child entity collection by providing index (location) in owner collection

document.Section[0].Body.ChildEntities.RemoveAt(0);

We have prepared a simple sample to illustrate this usage in below link. 
Sample Link:
GenerateWord.zip

If you are using mail merge with Groups in your application, there is an alternate solution by simply setting RemoveEmptyGroup property of MailMerge class to true and place the table in the input template within Begin and End Group fields, DocIO will handle the logic of removing empty groups (elements between Begin and End Group) when the data table is empty.

Code snippet:

//Removes Empty Group during Mailmerge           

document.MailMerge.RemoveEmptyGroup = true;

We have prepared a simple sample to illustrate this usage in below link. 
Sample Link:
GenerateWord_MailMerge.zip

kindly try running the above samples and let us know if this helps you.

Regards,
Ananda Kumar V




OL Olivier Lesage November 8, 2013 05:33 PM UTC

Could you make sample files available for download please ? We have this message : 
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>199E58130EAC18DC</RequestId>
<HostId>
ZBdJKTT0BaW224/wFlzzDOAtqZuOFmQdYRcpbnOy28+psyqa6tD7SX772ODg4tlM
</HostId>
</Error>
Thanks a lot

We try the function RemoveEmptyGroup in this sample but it doesn't wotk like we want.
The sample :
"BeginGroup:Test"
The text "Fusion"
"EndGroup:Test"
We would nothing in the file when there is no "Test" (ie Test = null).
But we obtain :
The text

Thanks.


SK Sathish K Syncfusion Team November 13, 2013 06:16 PM UTC

Hi Silvester,

 

Thank you for your update.

 

We have prepared the sample to illustrate your requirement to remove the document contents which is preserved in between the Merge field of BeginGroup and EndGroup when the DataTable doesn’t have any records. Please do find the sample from the attachment, try running the sample and let us know if this helps you.

 

Please let us know if you have any other questions.

 

Regards,

Sathish



GenerateWord_ca01c495.zip


BA Badri February 7, 2014 04:59 PM UTC

I am unable to download the sample solutions provided. Getting "Failed-Server Problem" when trying to download. Can you please help?


SK Sathish K Syncfusion Team February 10, 2014 07:23 AM UTC

Hi Badri,

 

Thank you for your interest in Syncfusion products.

 

Please find the attached sample which demonstrates how to remove the items preserved between the Merge field of BeginGroup and EndGroup when the DataTable doesn’t have any records.

 

Please let us know if you have any other questions.

 

Regards,

Sathish


Attachment: GenerateWord_133d802b.zip

Loader.
Live Chat Icon For mobile
Up arrow icon