Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Nov 25, 2017 3:49 pm

Dear support team,
Kindly provide VB.Net script for removing all tables from Word doc and saving changes to same file.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Mon Nov 27, 2017 2:01 am

Hello,

Thanks for your inquiry.
Please use the code below to remove tables in a document.
Code: Select all
'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)


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Nov 27, 2017 6:13 pm

Thanks a lot.

There is one immediate error message that prevents the rebuild of the solution - the word "table" in the code provided is ambiguous so I changed it to Spire.Doc.Table - I will check if it works properly and let you know

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Mon Nov 27, 2017 6:54 pm

This code does not work - Please re-check / fix and also provide a code that supports both DOCX and DOC.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Tue Nov 28, 2017 3:06 am

Hello,

Thanks for your response.
Please refer to the code below.
Code: Select all
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

If there's still any issue, please provide your sample file.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Nov 28, 2017 3:20 pm

I tweaked the piece of code provided and it works fine now.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Wed Nov 29, 2017 1:15 am

Hello,

Glad to hear that.
Just feel free to contact us if you need any assistance.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc

cron