How to remove table from document using C#

Hi Aspose guys,

Is used aspose word to remove some specific tables from doc file. Here is my code:

string file = @"C:\inputFile.doc";

Document doc = new Document(file);
doc.UpdateFields();
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    bool isRemove = false;
    // Iterate through all rows in the table.
    foreach (Row row in table.Rows)
    {
        // Iterate through all cells in the table.
        foreach (Cell cell in row.Cells)
        {
            foreach (Paragraph p in cell.Paragraphs)
            {
                Style stype = p.ParagraphFormat.Style;
                if (stype.Name.ToLower().StartsWith("resite_linktable_"))
                {
                    isRemove = true;
                }
            }
        }
    }

    if (isRemove)
    {
        table.Remove();
    }
}

doc.Save(@"C:\outputFile.doc");

But when I opened the “outputFile.doc”, word informed that “Word has detected a problem with this file. Opening it may be dangerous. You should not open this file unless you trust it.”.
I accepted to open, it was opened in protected view and tables were removed.

SO, how to remove tables correctly and there is no error with doc file?

Hi Vuong,

Thanks for your inquiry. Could you please attach your input Word document (inputFile.doc) here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hi awais.hafeez,

My code is correct, isn’t it? And here is my file for investigating.

Thanks for any help

Hi
Vuong,

Thanks for your inquiry. After an initial test with Aspose.Words v.10.8.0, I was unable to reproduce this issue on my side. Moreover, the code you shared earlier was correct.

Most likely this problem occurs because you have installed Office File Validation add-in in your computer. I tried opening the output document in MS WORD 2007 without OFV add-in being installed and haven’t observed any such issue.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi awais.hafeez,

I used Aspose.Word 11 with above code and file, I still got the issue(The file is opened in protected view in Word 2010). Even thought I just loaded .doc file then saved immediately, I still got the issue.

Some important NOTES:

  1. My .doc file is in WordprocessingML format
  2. All options of protected view in Word 2010 in my computer are enabled

Would you please enable protected view in Word 2010 in your computer and test again.

Thanks for any help

Hi Vuong,

Thank you for the additional information and sorry for the delayed response. I am afraid, I was still unable to reproduce this problem on my side. Could you please attach here a screen shot which shows the problem you’re facing?

Best Regards,

Hi awais.hafeez,

Here is my screen shots which show the problem.

1.jpg: Message from word 2010 when I opened doc file

2.jpg: When I clicked “Open”

Thanks and regards,

Hi awais.hafeez,

Here is screen shots which show the problem.

1.jpg: When I opened doc file

2.jpg: When I clicked “Open”

Thanks and regards

Hi Vuong,

Thank you for the additional information. While using latest Aspose.Words v.11.0.0 and Word 2010, i am unable to reproduce this issue. I have attached output file, please open this file on your side and share states.