How to check value of table cell in VB

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have some VB code that will loop through all tables in a document and
perform certain formatting functions on selected tables. To decide which are
the selected tables I need to test a cell.

How can I test the value of a cell? I have tried the following...

Dim pCell As Word.Cell
Dim pTable As Word.Table
For Each pTable In ActiveDocument.Tables
MsgBox pTable.Cell(2,1) 'This works
If pTable.Cell(2, 1) = "MyValue" Then 'This does NOT work
.... formatting instructions
End If
Next
 
Hi Bill

To retrieve the text from a table cell use something like
pTable.Cell(2,1).Range.Text

However, be aware that a table cell holds its text or other content plus the
end-of-cell marker. That consists of two characters: chr(13) & chr(7). So if
your table cell contained the text "Hi" then
len(pTable.Cell(2,1).Range.Text) = 4. And pTable.Cell(2,1).Range.Text = "Hi"
will be False. So you need to test bearing the end-of-cell marker in mind.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

Ask a Question

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.

Ask a Question

Back
Top