Extract value from table cell

T

terrchen

Hi,

Does anybody know how you can extract the text value from a table cell
using VB so that I can use it in a string comparison.

Thanks,
Terrence
 
H

Helmut Weber

Hi Terrence,

for VBA (!) from Word:

s = activedocument.tables(1).cell(4).range.text
s = left(s,len(s)-2)
msgbox s

From VB it would look similar, but with a word object
and possible a word-document-object in addition.

Probably the two characters chr$(13) + chr$(7),
which are used for end-of-cell and end-of-row
are causing you trouble.


Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

terrchen

Thanks.

I figured out how to do this but I highly doubt this is the most
efficient way. Basically what I did was select the text in the table
cell that I wanted. What this does is it makes it avalible via the
selection object. Once in the selection object I can use the Text
method to access the string.

For example

# Select the first cell in the first row
MyTable.Cell(1, 1).Select

# Assign the section text to the string cellvalue
cellvalue = Selection.Text

Terrence
 
D

Doug Robbins

Dim myrange as Range
Set myrange = ActiveDocument.Tables(n).Cells(i , j).Range
myrange.End = myrange.End-1
MsgBox myrange

myrange will contain the text in row i, column j of the nth table in the
active document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

On 22 Aug 2005 09:44:38 -0700, (e-mail address removed) wrote:
Hi Terrence,

modified example:

Dim s As String
s = ActiveDocument.Tables(1).Cell(1, 2).Range.Text
s = Left(s, Len(s) - 2)

And note the difference to Doug's example.

On the surface, more or less, where Doug's code operates,
the end-of-cell mark is one (!) character,
whereas if you put all of the cell's range text into a variable,
you have to cut off two (!) characters.

BTW there is a minor typo in Doug's code, as it happens.
Set myrange = ActiveDocument.Tables(n).Cells(i , j).Range ' no
Set myrange = ActiveDocument.Tables(n).Cell(i , j).Range ' yes

So you got two solutions. ;-)

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

terrchen

Since we are on this topic. Is there anyway I can do string
manipulations in VB for Microsfot word. For example, can I use regular
expressions to manipulate a string?
 
J

Jay Freedman

Since we are on this topic. Is there anyway I can do string
manipulations in VB for Microsfot word. For example, can I use
regular expressions to manipulate a string?

There are the usual VB functions/operators such as Left, Right, Mid, Instr,
and Like. Starting with Word 2000 there are also Split, Join, and Array
which can be useful for this.

You can go to Tools > References and add a reference to the Microsoft
VBScript Regular Expressions 5.5 library. That lets you use the RegExp
object's methods and properties -- see
http://msdn.microsoft.com/library/en-us/script56/html/vsobjRegExp.asp.
 

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

Top