Table Cells to Text?

J

JohnJohn

Hello.

Many of you may find this question moronic. I'm OK with that.

I'm writing a macro in a Word document (using VBA - Visual Basic editor)
that grabs the text values from a particular column in a table, one row at a
time, performs a couple of actions on the text, and writes them to a text
file. So I access the Cells collection of the table and cycle through the
same cell on all rows of the table.

I need to convert the cell contents to a string (there aren't even any
numbers or anything), and for the life of me, can't figure out how. I get a
"type mismatch" error when I assign the contents to a string variable, or
when I try to perform any kind of string-related function on it (such as
CStr, Replace, etc).

Meanwhile, it's OK if I just do a MsgBox Tables(1).Cell(2, 3) for example.
But that's all I can do with it. I just need to stuff the contents of that
cell into a string.

What obvious little nugget of information am I missing here?

Thanks!
John
 
J

Jean-Guy Marcil

JohnJohn was telling us:
JohnJohn nous racontait que :
Hello.

Many of you may find this question moronic. I'm OK with that.

I'm writing a macro in a Word document (using VBA - Visual Basic
editor) that grabs the text values from a particular column in a
table, one row at a time, performs a couple of actions on the text,
and writes them to a text file. So I access the Cells collection of
the table and cycle through the same cell on all rows of the table.

I need to convert the cell contents to a string (there aren't even any
numbers or anything), and for the life of me, can't figure out how.
I get a "type mismatch" error when I assign the contents to a string
variable, or when I try to perform any kind of string-related
function on it (such as CStr, Replace, etc).

Meanwhile, it's OK if I just do a MsgBox Tables(1).Cell(2, 3) for
example. But that's all I can do with it. I just need to stuff the
contents of that cell into a string.

What obvious little nugget of information am I missing here?

You must use the Text property of the cell range to manipulate it. Here is a
little example:

Dim cellRge As Range

Set cellRge = ActiveDocument.Tables(1).Cell(2, 3).Range

'to remove the end of cell marker from the range (¤)
cellRge.MoveEnd wdCharacter, -1

With cellRge
.Text = Replace(.Text, "123", "456")
End With


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

JohnJohn

Excellent!

Thank you.

Jean-Guy Marcil said:
JohnJohn was telling us:
JohnJohn nous racontait que :


You must use the Text property of the cell range to manipulate it. Here is a
little example:

Dim cellRge As Range

Set cellRge = ActiveDocument.Tables(1).Cell(2, 3).Range

'to remove the end of cell marker from the range (¤)
cellRge.MoveEnd wdCharacter, -1

With cellRge
.Text = Replace(.Text, "123", "456")
End With


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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