Dear support team,
Kindly provide VB.Net script for removing all tables from Word doc and saving changes to same file.
Thank you!
'Load Document
Dim doc As New Document()
doc.LoadFromFile("TableTest.docx")
'Remove all the tables
For Each s As Section In doc.Sections
Dim secTables As New List(Of Table)()
For Each tb As Table In s.Tables
secTables.Add(tb)
Next
For i As Integer = 0 To secTables.Count - 1
s.Tables.Remove(secTables(i))
Next
Next
'Save Document
doc.SaveToFile("RemoveTable.docx", FileFormat.Docx)
Private Sub button15_Click(sender As Object, e As EventArgs)
Dim path__1 = ""
Dim fileExts As String = ""
Dim openFileDialog As New OpenFileDialog()
If openFileDialog.ShowDialog() = DialogResult.OK Then
path__1 = openFileDialog.FileName
fileExts = Path.GetExtension(path__1)
End If
Dim doc As New Document(path__1)
For Each s As Section In doc.Sections
'1. remove the tables in the header
Dim hTable As New List(Of Table)()
For Each hObject As DocumentObject In s.HeadersFooters.Header.ChildObjects
If TypeOf hObject Is Table Then
Dim table As Table = TryCast(hObject, Table)
hTable.Add(table)
End If
Next
For i As Integer = 0 To hTable.Count - 1
s.HeadersFooters.Header.ChildObjects.Remove(hTable(i))
Next
'2. remove the tables in the footer
Dim fTable As New List(Of Table)()
For Each fObject As DocumentObject In s.HeadersFooters.Footer.ChildObjects
If TypeOf fObject Is Table Then
Dim table As Table = TryCast(fObject, Table)
fTable.Add(table)
End If
Next
For i As Integer = 0 To fTable.Count - 1
s.HeadersFooters.Footer.ChildObjects.Remove(fTable(i))
Next
'2. remove tables in body
Dim secTables As New List(Of Table)()
For Each tb As Table In s.Tables
secTables.Add(tb)
Next
For i As Integer = 0 To secTables.Count - 1
s.Tables.Remove(secTables(i))
Next
Next
doc.SaveToFile(Convert.ToString("result") & fileExts, FileFormat.Auto)
System.Diagnostics.Process.Start(Convert.ToString("result") & fileExts)
End Sub