Getting unfomatted text from a table cell

J

John

I'm using Selection.Tables(1).Cell(1, 1).Range.Text to find the
contents of a table cell. The problem I'm having is that as well as
getting the text, it gets some strange character at the end of the
cell (the end-of-cell marker?). How do I stop this from happening?
 
J

Jean-Guy Marcil

John was telling us:
John nous racontait que :
I'm using Selection.Tables(1).Cell(1, 1).Range.Text to find the
contents of a table cell. The problem I'm having is that as well as
getting the text, it gets some strange character at the end of the
cell (the end-of-cell marker?). How do I stop this from happening?

These are the characters Word uses to mark the end of a cell or a row (¤).
For Word it is a single character, when building strings, the compiler
treats it as 2 characters. Try this to remove those 2 characters from the
string:

Dim cellTextStr As String

With Selection.Tables(1).Cell(1, 1).Range
cellTextStr = Left(.Text, Len(.Text) - 2)
End With

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

John

Thanks, I'll try that.

On a related topic, is there a way to find the end-or-cell/row
character when using Word find/replace (eg ^p is paragraph)
 
J

Jean-Guy Marcil

John was telling us:
John nous racontait que :
Thanks, I'll try that.

On a related topic, is there a way to find the end-or-cell/row
character when using Word find/replace (eg ^p is paragraph)

No, you can't. The ¤ is actually 2 characters: Chr(7) and Chr(13) whereas
the ¶ is one character: Chr(13).

What do you want to do?

For example, here is some code that deletes trailing ¶ in all tables (Thanks
to Klaus Linke):
'_______________________________________
Dim tableLoop as Table
Dim cellLoop As Cell
For each tableLoop in ActiveDocument.tables
For Each cellLoop In tableLoop.Range.Cells
While Right(cellLoop.Range.Text, 3) = Chr(13) & Chr(13) & Chr(7)
cellLoop.Range.Characters.Last­.Previous.Delete
Wend
Next cellLoop
Next tableLoop
'_______________________________________

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

John

Hi,

Thanks very much for your help Jean-Guy.

I'm trying to read the contents of a cell into a string variable,
then:
1) replace all paragraph marks, manual line feeds, tabs and
non-breaking spaces with the space character
2) remove leading + trailing space characters
3) replace multiple space characters with a single space character
4) replace em and en dashes with a hyphen

I thought about using find and replace, but I don't think this works
with a string variable.

So, Chr(13) I assume is the paragraph mark. What are all the others
(manual line feed, em-dash, etc?) Is there a list of them in any of
the VBA help files?
 
J

Jean-Guy Marcil

John was telling us:
John nous racontait que :
Hi,

Thanks very much for your help Jean-Guy.

I'm trying to read the contents of a cell into a string variable,
then:
1) replace all paragraph marks, manual line feeds, tabs and
non-breaking spaces with the space character
2) remove leading + trailing space characters
3) replace multiple space characters with a single space character
4) replace em and en dashes with a hyphen

I thought about using find and replace, but I don't think this works
with a string variable.

So, Chr(13) I assume is the paragraph mark. What are all the others
(manual line feed, em-dash, etc?) Is there a list of them in any of
the VBA help files?

Select the character you want (A tab for example),
then in the VBA editing window, display the Immediate window (CTRL-G) and
type the following in this window:

? asc(selection.Text)

Then hit Enter.

You get 9. So a Tab is Chr(9).

And so on.

Make sure not to delete the Chr(13) that comes with the Chr(7), i.e the last
the Chr(13)....

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

John

Thanks again Jean-Guy.

What is the problem with deleting the Chr(13) ? I'm not going to write
the string back into a cell, so I don't see why I need this paragraph
mark. Or does VBA use it to indicate end of string (a bit like C uses
Ascii 0 to mark the end of a string)?
 
J

Jean-Guy Marcil

John was telling us:
John nous racontait que :
Thanks again Jean-Guy.

What is the problem with deleting the Chr(13) ? I'm not going to write
the string back into a cell, so I don't see why I need this paragraph
mark. Or does VBA use it to indicate end of string (a bit like C uses
Ascii 0 to mark the end of a string)?

Ha! I did not know what you were doing with the string after cleaning up. I
thought you were putting it back in the cell, but "clean". If you are going
to use the string elsewhere as a pure string of characters, then of course
you can delete the last Chr(13)Chr(7).

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

John

OK, thank again.

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


Ha! I did not know what you were doing with the string after cleaning up. I
thought you were putting it back in the cell, but "clean". If you are going
to use the string elsewhere as a pure string of characters, then of course
you can delete the last Chr(13)Chr(7).

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

Klaus Linke

Hi John,

Just to make sure: You know the Replace function?
say,
Dim strTest As String
strTest = Selection.Cells(1).Range.Text
strTest = Replace(strTest, ChrW(13) & ChrW(7), "")

Regards,
Klaus
 

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