Character test

C

Compass Rose

I want to check if the last character in cell (i,2) of table 2 is a space and
delete it if it is. The coding I'm using is as follows:

Set Tbl = ActiveDocument.Tables(2)
For i = 2 To Tbl.Rows.Count
Tbl.Cell(i, 2).Select
Selection.EndKey Unit:=wdLine, Extend:=wdMove
LastChar = Asc(Selection.MoveLeft(Unit:=wdCharacter, Count:=1,
Extend:=wdExtend))
If LastChar = 32 Then Selection.Delete
Next i

LastChar always equals 49, regardless of what the last character is. Where
have I gone wrong?

TIA
David
 
J

Jonathan West

Compass Rose said:
I want to check if the last character in cell (i,2) of table 2 is a space
and
delete it if it is. The coding I'm using is as follows:

Set Tbl = ActiveDocument.Tables(2)
For i = 2 To Tbl.Rows.Count
Tbl.Cell(i, 2).Select
Selection.EndKey Unit:=wdLine, Extend:=wdMove
LastChar = Asc(Selection.MoveLeft(Unit:=wdCharacter, Count:=1,
Extend:=wdExtend))
If LastChar = 32 Then Selection.Delete
Next i

LastChar always equals 49, regardless of what the last character is. Where
have I gone wrong?


The last character is always an end of cell marker. Try the one before.
 
C

Compass Rose

If I view the document while I step through the code (F8), I can see that the
last character of the cell is selected, not the end of cell marker. Using a
MsgBox, I can view the value of LastChar, and it always returns 49. I'm
stumped.

David
 
J

Jay Freedman

Hmm, interesting mistake. If you check the VBA Help topic on the MoveLeft
method, you'll find that its return value is the number of units (in this
case, characters) the selection was moved -- that is, the return is not the
Selection itself, as you seem to expect. Since you've moved left by 1
character, the return value is always 1. Because the Asc function takes a
string argument, the integer 1 is implicitly converted to a character "1",
and the ASCII value of that is 49. Bingo!

Once you get the correct logic for that (move the left end of the Selection,
then take the Asc value of the Selection), you'll find that Word has another
surprise waiting for you: If you select a space character at the end of a
table cell, Selection.Delete does nothing. That's also true in the user
interface; select that space and hit the Delete key, and the space doesn't
go away. It's also true if the macro sets a Range object to cover the space
and tries to delete the range. The only thing that works is to put the
Selection to the right of the space and type a backspace.

Set Tbl = ActiveDocument.Tables(2)
For i = 2 To Tbl.Rows.Count
Tbl.Cell(i, 2).Select
Selection.EndKey unit:=wdLine, Extend:=wdMove
Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
LastChar = Asc(Selection)
If LastChar = 32 Then
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
End If
Next i

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Doug Robbins - Word MVP

Use the Trim function

Dim myrange As Range
Set myrange = ActiveDocument.Tables(1).Cell(1, 1).Range
myrange.End = myrange.End - 1
myrange.Text = Trim(myrange.Text)


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

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