Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

Extract value from table cell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
terrchen@gmail.com - 22 Aug 2005 16:53 GMT
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
Helmut Weber - 22 Aug 2005 17:22 GMT
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"
terrchen@gmail.com - 22 Aug 2005 17:44 GMT
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
Helmut Weber - 23 Aug 2005 11:57 GMT
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"
Doug Robbins - 22 Aug 2005 17:57 GMT
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.

Signature

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

> Hi,
>
[quoted text clipped - 3 lines]
> Thanks,
> Terrence
terrchen@gmail.com - 23 Aug 2005 18:48 GMT
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?
Jay Freedman - 23 Aug 2005 19:56 GMT
> 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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.