Determine if last character is a space

S

Steve D

How do I determine if the last character (before the paragraph return) is a
space? Thanks.
 
G

Greg

Steve,

You could do it with a string comparison. E.g.,

Sub ScratchMacro()

Dim myRange As Range
Set myRange = Selection.Range
If InStr(myRange.Text, Chr(13)) = 0 Then
MsgBox "First select text containing a paragraph mark"
Exit Sub
ElseIf Right(myRange.Text, 2) = " " & Chr(13) Then
MsgBox "The character before the PM is a space."
Else
MsgBox "The character before the PM is not a space."
End If
End Sub
 
G

Greg

Steve,

You could do it with a string comparison. E.g.,

Sub ScratchMacro()

Dim myRange As Range
Set myRange = Selection.Range
If InStr(myRange.Text, Chr(13)) = 0 Then
MsgBox "First select text containing a paragraph mark"
Exit Sub
ElseIf Right(myRange.Text, 2) = " " & Chr(13) Then
MsgBox "The character before the PM is a space."
Else
MsgBox "The character before the PM is not a space."
End If
End Sub
 
S

Steve D

Thanks, Greg. That will work.

I was trying to get at the next-to-last character (before the CR) with the
"last" property.

Why does this work...
Set MyRange = ActiveDocument.Paragraphs(1).Range.Characters.Last

But this won't work...
NumChars = ActiveDocument.Paragraphs(1).Range.Characters.Count
Set MyRange = ActiveDocument.Paragraphs(1).Range.Characters(NumChars -
2)

I've got an appropriate index to the Characters.
 
J

Jean-Guy Marcil

Steve D was telling us:
Steve D nous racontait que :
Thanks, Greg. That will work.

I was trying to get at the next-to-last character (before the CR)
with the "last" property.

For example, let's say you have a paragraph like this:

123¶
Why does this work...
Set MyRange = ActiveDocument.Paragraphs(1).Range.Characters.Last

Here MyRange = ¶
or Chr(13)
But this won't work...
NumChars = ActiveDocument.Paragraphs(1).Range.Characters.Count
Set MyRange =
ActiveDocument.Paragraphs(1).Range.Characters(NumChars - 2)

Here MyRange = 2

I got the impression you were targeting the "3" in my example. So you need

ActiveDocument.Paragraphs(1).Range.Characters(NumChars - 1)

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

Jonathan West

Steve D said:
Thanks, Greg. That will work.

I was trying to get at the next-to-last character (before the CR) with the
"last" property.

Why does this work...
Set MyRange = ActiveDocument.Paragraphs(1).Range.Characters.Last

But this won't work...
NumChars = ActiveDocument.Paragraphs(1).Range.Characters.Count
Set MyRange = ActiveDocument.Paragraphs(1).Range.Characters(NumChars -
2)

I've got an appropriate index to the Characters.

You want the Last.Previous property to pick up the character you want

If Asc(ActiveDocument.Paragraphs(1).Range.Characters.Last.Previous) = 32
Then
'char is a space
Else
'it isn't
End If
 
K

Klaus Linke

Jean-Guy Marcil said:
I got the impression you were targeting the "3" in my example. So you need

ActiveDocument.Paragraphs(1).Range.Characters(NumChars - 1)

or
ActiveDocument.Paragraphs(1).Range.Characters.Last.Previous

Greetings,
Klaus
 
G

Greg

Steve.

I don't see how
ActiveDocument.Paragraphs(1).Range.Characters.Last
evaluates the character before the paragraph mark. That line returns
the paragraph mark itself. You could use that in:

Sub ScratchMacro()

Dim myRange As Range
Dim numChr As Long


Set myRange = Selection.Paragraphs(1).Range.Characters.Last
myRange.MoveStart Unit:=wdCharacter, Count:=-1
myRange.MoveEnd Unit:=wdCharacter, Count:=-1

'Or this
'numChr = Selection.Paragraphs(1).Range.Characters.Count
'Set myRange = Selection.Paragraphs(1).Range.Characters(numChr - 1)

If myRange.Text = " " Then
MsgBox "The character before the PM is a space."
Else
MsgBox "The character before the PM is not a space."
End If

End Sub
 

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