none
Deleting empty tables in document RRS feed

Answers

  • Try this:

    Sub Demo()
     Application.ScreenUpdating = False
     Dim i As Long, StrTxt As String
     With ActiveDocument
       For i = .Tables.Count To 1 Step -1
         StrTxt = Replace(.Tables(i).Range.Text, Chr(7) & Chr(13), vbNullString)
         If Len(StrTxt) = 2 Then .Tables(i).Delete
       Next
     End With
     Application.ScreenUpdating = True
     End Sub


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm


    Friday, November 22, 2013 8:18 PM

All replies

  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long, StrTxt As String
    With ActiveDocument
      For i = .Tables.Count To 1 Step -1
        StrTxt = Replace(.Tables(i).Range.Text, Chr(7) & Chr(13), vbNullString)
        If StrTxt = Chr(13) Then .Tables(i).Delete
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Friday, November 22, 2013 12:12 PM Code revision
    Friday, November 22, 2013 12:09 PM
  • Nothing happens in the document when I run that macro.l
    Friday, November 22, 2013 1:29 PM
  • Try this:

    Sub Demo()
     Application.ScreenUpdating = False
     Dim i As Long, StrTxt As String
     With ActiveDocument
       For i = .Tables.Count To 1 Step -1
         StrTxt = Replace(.Tables(i).Range.Text, Chr(7) & Chr(13), vbNullString)
         If Len(StrTxt) = 2 Then .Tables(i).Delete
       Next
     End With
     Application.ScreenUpdating = True
     End Sub


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm


    Friday, November 22, 2013 8:18 PM
  • That suggests the tables aren't truly empty. Are you sure they don't contain spaces, tabs, etc.?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 22, 2013 8:21 PM
  • Paul,

    It seems that after running your code you are left with a single end of cell mark or a "dot" if you call a msgbox.  That is why I changed your If StrTxt to a length check.

    It worked after the change, not before and yes I am sure my tables were empty.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Friday, November 22, 2013 8:34 PM
  • Hi Greg,

    In my testing, the code as posted worked fine with empty tables. Having re-started Word, it now seems not to. Oh, well...


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 22, 2013 11:50 PM