Re: Interesting Observation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Greg: Just some additional info, FWIW

When I tried to put your observation into an If statement as such:
If rng.Characters.Last.Next = chr$(13) & chr$(7) Then
rng.MoveEnd Unit:=wdCharacter, Count:=-2
End If
it actually gave me the first character of the next table cell!

I changed to:
If rng.Characters.Last= chr$(13) & chr$(7) Then
rng.MoveEnd Unit:=wdCharacter, Count:=-2
End If
and it returned the correct response - except the "-2" count went one
character past the marker. Byt changing that to "-1", I was able to reset
the range to cover only the text in a cell without the marker. Apparently,
the end of cell marker consists of two characters, but is seena dn counted
by Word as only one character.

Ed

"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:%23LehTKcKGHA.536@xxxxxxxxxxxxxxxxxxxxxxx
Peter,

That is interesting.

While:
Select Case Asc(oRng.Characters.Last.Next)
Case 13

does detect the end of cell marker
and

Select Case oRng.Characters.Last.Next
Case Chr(13)

doesn't detect the marker

Select Case oRng.Characters.Last.Next
Case Chr$(13) & Chr$(7)

does!!


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Peter Jamieson wrote:
Hi Greg,

The documentation suggestions that "Last" returns the last
/character/ when applied to "Characters", but in fact it returns a
range containing two characters, the first of which is a 13 and the
second of which is a 7. I won't attempt to check whether that, and
the behaviour of Chr etc. is consistent with your results, but maybe
that's enough for you to make progress (don't forget that there are
also AscW and ChrW functions for delaing with wide/Unicode characters.

Peter Jamieson

"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:%23IGeN1ZKGHA.536@xxxxxxxxxxxxxxxxxxxxxxx
Doug,

That method process patterns that I don't want processed. I don't
want to process number patterns like:

1.800.867.5309 or 2/04/2006 or 123-45-6789 or 123DEF123456798OT etc.

The only way I can see to do that is compare the character before and
after the found range to "space" separators like tabs, spaces, line
breaks, end of cell marks, etc.

I found it interesting that:

Case Select Asc(oRng.Characters.Last.Next)
Case 13

detects and end of cell while

Case Select oRng.Characters.Last.Next
Case Chr(13)

doesn't.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Doug Robbins - Word MVP" <dkr@xxxxxxxxxxxxxxxxxx> wrote in message
news:%23DplMmXKGHA.140@xxxxxxxxxxxxxxxxxxxxxxx
Hi Greg,

I would use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<[0-9]{4}>", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = myrange.Start - 1
If Not Left(myrange, 1) = "-" Then
myrange.Start = myrange.Start + 1
myrange = Format(myrange, "#,###")
Else
myrange.Collapse wdCollapseEnd
End If
Loop
End With

Seems to me it does what you want even if the number is in a table.
--
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

"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:eOvlq9SKGHA.3876@xxxxxxxxxxxxxxxxxxxxxxx
I was playing around with some code to format simple numbers like
1234 as 1,234 etc. I didn't want to process numbers like phone
numbers 1-800-867-5309, or SSNs 123-45-6789 or serial numbers like
12345ERT56789WWQ1234 etc. I figured I could first find a group of
4 or more numbers and then check the preceeding and following
characters and only process appropriate ranges.

In the code below, you will notice that I used
Asc(oRng.Characters.Last.Next) and
Asc(oRng.Characters.First.Previous) in my Select Case statements.

At first I tried just oRng.Characters.Last.Next and
oRng.Characters.First.Previous and then usied Case Chr(7), Chr (9),
etc., in the Case statements. To my dismay I discovered that it
was not processing numbers found in a table cell. I am not sure,
but I have heard that an end of cell marker is a composite Chr(7)
and Chr(13). I just figured that .Next or .Previsous would be
either a Chr(7) or Chr(13). When it didn't work I used: MsgBox
Asc(oRng.Characters.Last.Next) in a Case Else statement and
confirmed that "13" was returned. With this knowledge I changed
the code accordingly.

I think it is interesting that it works one way and not the other
when on the surface it looks the same. There must be some type of
conversion going on with the Asc statement that I don't understand.
If anyone cares to enlighten, I am all attention. Thanks.

Sub CommaFormatNumbers()

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([0-9]{4,})"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
Select Case Asc(oRng.Characters.Last.Next)
Case 7, 9, 10, 11, 13, 32
On Error GoTo Handler
Select Case Asc(oRng.Characters.First.Previous)
Case 7, 9, 10, 11, 13, 32
Proceed:
oRng = Format$(oRng, "#,##0")
oRng.Collapse wdCollapseEnd
Case Else
oRng.Collapse wdCollapseEnd
End Select
Case Else
'Do Nothing
End Select
Wend
End With
Exit Sub
Handler:
Resume Proceed
End Sub





--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.




.



Relevant Pages

  • Re: Interesting Observation
    ... Greg Maxey/Word MVP ... also AscW and ChrW functions for delaing with wide/Unicode characters. ... breaks, end of cell marks, etc. ... but I have heard that an end of cell marker is a composite Chr ...
    (microsoft.public.word.vba.general)
  • Re: Rotated Text Help Needed
    ... cell heights as you said happened on your XP. ... > characters such that the top left corner of the character cell sits at the ... > character cell as well as the character glyph itself. ... > Private Const LF_FACESIZE = 32 ...
    (comp.lang.basic.visual.misc)
  • Re: Formatting index entries
    ... replace each space with a tab character ... Copy the whole cell ... use Edit>Replace to replace every tab with a space. ... you want to rip all the Index tags out of the main document. ...
    (microsoft.public.mac.office.word)
  • Re: Carriage return in same cell without using alt-enter
    ... I still think that the OP is underestimating the user community. ... to change this unique character to the alt-enter. ... Here is the code now in the custom Sub: ... I have managed to intercept the ENTER keypress whilst editing the cell with: ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Carriage return in same cell without using alt-enter
    ... The event code I posted uses the [Chras the de-limiter that gets ... to change this unique character to the alt-enter. ... Here is the code now in the custom Sub: ... I have managed to intercept the ENTER keypress whilst editing the cell with: ...
    (microsoft.public.excel.worksheet.functions)