Microsoft Excel - Delete word table if formfield in cell is blank

Asked By John Wirth on 22-Mar-13 08:06 PM
Hi- am wanting to loop through tables in a Word document, and if the formfield in cell (1, 2) is blank, then the table is deleted. The problem seems to be with identifying the formfield as being blank. I run the code and nothing happens, although if I specify a value that exists in one of the formfields, the code works and the table is deleted.

 I have tried Trim, Len, Null, IsEmpty, but none work.

Sub BlankFldsInTbl()
Set WdApp = Word.Application
Dim Tbl As Table
With WdApp.ActiveDocument
For Each Tbl In .Tables
If Tbl.Cell(1, 2).Range.Fields(1).Result = "" Then
WdApp.Selection.MoveRight Unit:=wdCharacter, Count:=1
End If
Next Tbl
End With
End Sub
Harry Boughen replied to John Wirth on 22-Mar-13 11:50 PM
Hi John,
This might be better posted in the Word Forum but try this.
If Len(Tbl.Cell(1, 2).Range.Text) = 0 Then

Sorry John, just realised you were using form fields so try using the Len() = 0 on your function or try using "" for the comparison.
John Wirth replied to Harry Boughen on 23-Mar-13 06:09 AM
Thanks Harry, I tried both suggestions- the first one does nothing, the second one hits runtime error 13 "type mismatch".

If I try getting the contents of the cell and showing in a message box with the use of Len as you suggested, the message box shows 7, if I use MsgBox Tbl.Cell(1, 2).Range.Text I get the square symbolizing a non printing character.
Harry Boughen replied to John Wirth on 23-Mar-13 07:20 AM
Hello John,
Interesting that the Len gives you 7 suggesting that there are seven somethings in the field.  Perhaps if you carve up the field and look at the Codes to see what is actually there it might help to get a suitable test.
One other thought, could you try FormFields(1) instead of Fields(1)
Word is a bit out of my line so perhaps you might be better to try the Word group for assistance if that does not work.
John Wirth replied to Harry Boughen on 23-Mar-13 01:17 PM
Hi Harry, thank you so much- using FormFields works perfectly (no need for Trim. Now any of the 30 Word tables are deleted by Excel if the form field in the specified cell is blank.

Yes, I found it odd that the use of Trim when passed to a message box produced a seven- especially as when I chose to show all formatting marks, there are only five small circles in the formfield which is what I would expect.

Thanks again.