Strip End Cell Marker

G

Greg Maxey

Posted below are a few methods for stripping the end of cell marker from a
cell range. Realizing that in these simple examples speed is not an issue,
but I was wondering if anyone could offer any technical insight into which
method if any of these would be considered "best practice." Would be
interested in other methods anyone might be using as well.

Sub Test1()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
oRng.MoveEnd wdCharacter, -1
MsgBox oRng.Text
End Sub

Sub Test2()
Dim oStr As String
oStr = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
MsgBox Left(oStr, Len(oStr) - 2)
End Sub

Sub Test3()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
oRng.End = oRng.End - 1
MsgBox oRng.Text
End Sub

Sub Test4()
Dim oString As String
oStr = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
MsgBox Replace(oStr, ChrW(13) & ChrW(7), "")
End Sub
 
D

Doug Robbins

Hi Greg,

I always use

Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
oRng.End = oRng.End -1


--
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

Hi Greg,
I use Doug's method, too.
Beware of replacing.
It is possible in theory to have a paragraph
mark followed by chr(7) in a cell.

Though I've never seen such a combination,
except the one I've produced right now.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
G

Greg Maxey

Doug, Helmut

Thanks. I think it interesting that the cell marker has a length of 2
characters illustrated in example two, a width of only one wdCharacter as
illustrated in examples two and three, and in fact consists of two
characters as illustrated in example 4. Seems to disprove the thought that
two things can't occupy the same space at the same time :)
 
J

Jay

Hi Greg,

"a width of only one wdCharacter as illustrated in examples two and
three"
How does example two illustrate this?

Also, does Word internally store characters as 16-bit (eg unicode)?

Jay
 
J

Jonathan West

Greg Maxey said:
Doug, Helmut

Thanks. I think it interesting that the cell marker has a length of 2
characters illustrated in example two, a width of only one wdCharacter as
illustrated in examples two and three, and in fact consists of two
characters as illustrated in example 4. Seems to disprove the thought
that two things can't occupy the same space at the same time :)

The reasons for the apparent confusion is that the same name "character" is
being used for two completely different entities.

Within a string, a character is an unformatted single printable or
unprintable item. The item is a representation of an 8-bit ANSI or 16-bit
Unicode code as appropriate.

Within the document a Character is a particular kind of Range object,
containing formatting information as well as (optionally) printable text.

Converting a document Character to a string character always results in the
formatting information being stripped off, and sometimes results in one
character being included in the string, sometimes two.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg

You are right, it actually doesn't. I meant a width (or length) of two
characters. Len - 2
 
K

Klaus Linke

Helmut Weber said:
Hi Greg,
I use Doug's method, too.
Beware of replacing.
It is possible in theory to have a paragraph
mark followed by chr(7) in a cell.

Though I've never seen such a combination,
except the one I've produced right now.


Hi Helmut,

Sorry, I don't understand what you mean. If there were the possibility of a
paragraph mark followed by Chr(7) in the cell, it would be binary garbage.
Try to put a Chr(7) into a doc (with the Alt key, or with a macro), and
you'll see how hard it is to get it into the doc at all.
IMO, replacing the end-of-cell-marker with nothing is safer than just
stripping the last two characters. The latter depends on the current
implementation, which might change or depend on some future option setting,
the former doesn't.

Regards,
Klaus
 
K

Klaus Linke

Hi Greg,

Actually, I often use a fifth method:
oRng.MoveEndWhile Cset:=Chr(13) & Chr(7), Count:=wdBackward
http://groups.google.com/group/micr..._frm/thread/cdd39a61638f75bc/4ed4a3fba749bab3

All the methods work, and I only prefer the above because I like to be
explicit.
Someone who doesn't know that myCell.Range.Text includes the end-of-cell
marker doesn't understand why you'd strip the last two characters.
Also, since you can't manipulate the end-of-cell markers with code, I think
it was an error to have myCell.Range.Text contain them in the first place.
And I like my code to work independently of MS design errors.

;-) Klaus
 
T

Tony Jollans

I suspect this could go on for ever but ..

oRng.MoveEndWhile Cset:=Chr(13) & Chr(7), Count:=wdBackward

could strip user data (paragraph marks - I'm not really interested in user
chr(7)'s) which might be relevant depending on what you were doing with the
Range afterwards.
 
K

Klaus Linke

You are right, that's by design (and different from the other macros): I
actually always want to strip trailing paragraph marks. I'd even add all
whitespace characters, too, if I don't strip them in the further processing
anyway.

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