Get character after range variable

R

RB Smissaert

Trying to do the following:
Check the character after a range variable and if this is a space then
delete that range plus the space character after it.
I am not used to Word VBA and eventually I came up with this code that
works, but it seems there must be a simpler way:

Dim oRangeDup As Range

If vStringNilFound = -1 Then
Set oRangeDup = oRange.Duplicate
oRangeDup.Collapse wdCollapseEnd
oRangeDup.Expand wdCharacter
If oRangeDup.Text = " " Then
oRange.Expand wdWord
oRange.Delete
End If
Else
oRange.Text = vStringNilFound
End If

I need to leave the oRange variable intact if there is no space after that
oRange, so I can't collapse that
oRange.

Any suggestion what the normal way is to do this?


RBS
 
T

Tony Jollans

You should be able - unless you are at the end of the document - to check
the character after the last in the Range

If oRange.Characters.Last.Next.Text = Space(1) Then
oRange.MoveEnd wdCharacter, 1
oRange.Delete
End If
 
R

RB Smissaert

Thanks, that looks better and will use that.
How would you check if the end of the range variable is the end of the
document?

RBS
 
T

Tony Jollans

Checking the end of the Range against the end of the Document is the obvious
way, and there are a couple of ways to do that. My preference is generally
something like:

If oRange.End <> oRange.StoryLength Then
...

because this will work in the main body of the document, or the header, or
wherever you happen to be.
 
R

RB Smissaert

Thanks, that looks a good way.

RBS


Tony Jollans said:
Checking the end of the Range against the end of the Document is the
obvious way, and there are a couple of ways to do that. My preference is
generally something like:

If oRange.End <> oRange.StoryLength Then
...

because this will work in the main body of the document, or the header, or
wherever you happen to be.

--
Enjoy,
Tony

www.WordArticles.com
 

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