Results 1 to 6 of 6
  1. #1
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Selection - Is Char to Left a Space? (Word 2000 SR-3)

    I'm trying to work with Selection in Word 2000 VBA to determine if the character to the left of the cursor is a space or not. For the life of me, I can't get the syntax correct. Does anybody know the Selection code for this??

    Thx.

    -Rich Belthoff

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Selection - Is Char to Left a Space? (Word 2000 SR-3)

    Unfortunately, there is no built-in Selection.CharacterToTheLeft property or method. As you've probably figured out, the closest thing, Selection.Text, when the Selection is an insertion point, returns the one character to the right.

    You can get this character without disturbing your selection by creating a range over the character to the left of the selection. This is a simple example that isn't really well thought through. If you improve on it, please post back:

    Function GetCharLeft(ByVal rngStartFrom As Range) As String
    ' You must pass a range to this function, e.g., Selection.Range
    rngStartFrom.SetRange Start:=rngStartFrom.Start - 1, End:=rngStartFrom.Start
    GetCharLeft = rngStartFrom.Text
    End Function

    Sub TestGetCharLeft()
    MsgBox ">" & GetCharLeft(Selection.Range) & "<"
    End Sub

  3. #3
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Selection - Is Char to Left a Space? (Word 2000 SR-3)

    This works great for me. I don't think I can improve upon it. The only thing I did was add another function that called your function so I could use it several places in my macro. Here it is if it's any help (probably not, although I'm going to post my entire "numbers to words" macro in the thread I started on this issue in case anyone is interested):

    Function ClearExtraSpace()
    'Uses GetCharLeft() to determine if char to left is space
    'If so, it deletes it so that there is no extra space when
    'parenthetical numbers are inserted
    vSpaceTest = GetCharLeft(Selection.Range)
    If vSpaceTest = " " Then
    Selection.TypeBackspace
    End If
    End Function

    Thx.

    -Rich Belthoff

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection - Is Char to Left a Space? (Word 2000 SR-3)

    Not better, but maybe easier to read:
    <pre>If Selection.Characters.First.Previous=" " then</pre>

    Always fun to drill down the object model <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Selection - Is Char to Left a Space? (Word 2000 SR-3)

    Klaus, what can I say? Sheer genius. <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  6. #6
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Selection - Is Char to Left a Space? (Word 2000 SR-3)

    That's great! It works, and it's a little bit shorter.

    Thx.

    -Rich Belthoff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •