Re: How do I read the content or value of a table's cell in Word? Is there
a way to detect if a table's cell is empty or not?
I assume that you meant to post this question in the VBA section and not the
general questions section where I found it.
The following macro illustrates the method of reading a value from a table
cell. Note that an empty cell will always have a length of 2 characters. And
it is often a good idea to remove those two characters from a string before
using it.
Sub IsCellEmpty()
Dim s As String
Selection.Collapse CollapseEnd
If Selection.Information(wdWithInTable) = False Then
MsgBox "The cursor must be positioned in a table."
Exit Sub
End If
s = Selection.Cells(1).Range.Text
If Len(s) = 2 Then
MsgBox "Cell is empty."
Else
s = Left(s, Len(s) - 2)
MsgBox "Cell contains: " & s
End If
End Sub
Thanks for the help. You're right, being a novice (at posting) I posted to
wrong area. I meant for the VBA area. My goal is to search for first empty
cell in a table without using selection but within a tight loop (for speed).
Once found then I would move the selection to that cell. However, I can't
get VBA to allow me to directly test the cell contents or tell me whether the
cell is empty or not. Any further assistance will be appreciated too.
<< My goal is to search for first empty cell in a table without using
selection but within a tight loop (for speed). Once found then I would move
the selection to that cell. However, I can't get VBA to allow me to directly
test the cell contents or tell me whether the cell is empty or not. >>
Perhaps something like:
Sub CheckFirstCell()
Dim i As Long
If ActiveDocument.Tables.count = 0 Then Exit Sub
For i = 1 To ActiveDocument.Tables.count
If Len(ActiveDocument.Tables(i).Range.Cells(1).Range.Text) = 2 Then
MsgBox "First cell in table #" & i & " is empty."
Else
MsgBox "First cell in table #" & i & " is NOT empty."
End If
Next i
End Sub
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.