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

Free Support Forum - aspose.com

Including an optional table in Word document

Hi,
I have a word document which has a table in it that should not be displayed if there is no data in the table.
I think the way to achieve this would be to put the table in a bookmark and then remove the data in the bookmark if there is nothing to display.
I have attached a document for reference.
Here is a sample of code I have in VB to remove the table.

Dim doc As Document = New Document(templatePath)
doc.MailMerge.RemoveEmptyParagraphs = True
doc.Range.Bookmarks("LipidBookmark").Text = ""
doc.Range.Bookmarks("LipidBookmark").BookmarkStart.ParentNode.Remove()
doc.MailMerge.Execute(dataset)
'Save the document
doc.Save(LetterName, SaveFormat.Doc, SaveType.OpenInWord, Response)

However the table will always show in my resulting document.
The version of Words that I am using is 4.4.3.0
Any clues to where I have gone wrong would be greatly appreciated.
Joe

Hi
Thanks for your inquiry. I think that you can try using the following code:

Dim doc As Document = New Document("in.doc")
'Get table
Dim table As Node = doc.Range.Bookmarks("LipidBookmark").BookmarkStart.GetAncestor(NodeType.Table)
'Remove table if it exists
If (Not table Is Nothing) Then
table.Remove()
End If
'Save the document
doc.Save("out.doc")

I hope this helps.
Best regards.

Hello Alexey,
Thank you for your response.
I was just coming back to the forum now to say that I managed to achieve what I wanted with TableStart/TableEnd.
I will keep your suggestion for future reference. Thanks once again!
Joe

Hi
It is nice that you already found solution.
Best regards.