Results 1 to 5 of 5
  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

    Word 2010 Macro to Test for Space

    I have a macro that converts words to numbers. It pastes the results into a document. Here is the tail end of my code (the prior code uses fields to insert the "words" for numbers and the insertion is "selected" when I reach this part of the code, so it "unlinks" the fields and moves around a bookmark to delete the ~ character which I used as a location placeholder):

    Selection.Fields.Unlink 'Rather than pasting, "unlinks" field to it's just text
    With ActiveDocument.Bookmarks 'Surrounds selected text with bookmark
    .Add Range:=Selection.Range, Name:="NumBookMark"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Removes selected text
    Selection.TypeBackspace 'Deletes ~ character to left
    Selection.GoTo What:=wdGoToBookmark, Name:="NumBookMark" 'Re-selects bookmarked text
    With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    Selection.MoveRight Unit:=wdCharacter, Count:=1 'Moves right once to de-select text

    What I now need to do is check to see if the character to the right of the insertion point (which is now located just to the right of the inserted number "words" text). I tried some combination of this, but it doesn't check if there is a space to the right of the insertion point. I don't think I'm using the right query.
    If Selection.Characters.First.Next <> " " Then 'If char is a space '2012-02-20 Testing This Again
    Selection.TypeText Text:=" "
    Else
    GoTo SkipErrorMsg
    End If

    I either get an error or it skips to the errormsg. Anybody know what VBA code I need to use to determine if there is a space to the right of the insertion point? I have tried to find the answer already on the forum, but couldn't locate anything with the searches I was trying.

    Thanks!!

    -Rich

  2. #2
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    240
    Thanks
    3
    Thanked 24 Times in 24 Posts

    Thumbs up

    Is there a need to preserve the spaces? Here's a method that selects a text character, which you can then analyze and do whatever with, that removes the spaces automatically as you select the letter before it.

    Code:
        Selection.Extend
        Selection.MoveRight Unit:=wdCharacter, Count:=1  'insert test procedure on next line
        Selection.Delete Unit:=wdCharacter, Count:=1     'remove this line if you've already replaced character
    Alternatively you could search the selected text for spaces and replace them with some character which would not exist normally within a sentence, and work with that instead of the space.

  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
    Thanks for the code! I think where my code is failing, though, is in how I test to see if the selected character is a space. Are you saying that after your second line, I should insert the code I used to test for the space? Or what exactly would I insert for the test procedure on the next line? Thanks!

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I am not sure exactly what you want to achieve but the following code should demonstrate enough for you to achieve what you want. This code removes the spaces before and after the selected text and unlinks any fields within the selection. It also bookmarks the selection (not the field result) but makes no use of that bookmark. How this code works depends very heavily on what the selection contains when the code begins.

    Code:
      Dim aRng As Range, aBkmk As Bookmark
      Set aRng = Selection.Range
      Selection.Fields.Unlink 'Rather than pasting, "unlinks" field to it's just text
      ActiveDocument.Bookmarks.Add Range:=aRng, Name:="NumBookMark"
      Debug.Print aRng.Previous(Unit:=wdCharacter, Count:=1)
      While aRng.Previous(Unit:=wdCharacter, Count:=1) = " "
        aRng.Previous(Unit:=wdCharacter, Count:=1).Delete
      Wend
      While aRng.Next(Unit:=wdCharacter, Count:=1) = " "
        aRng.Next(Unit:=wdCharacter, Count:=1).Delete
      Wend
    Last edited by Andrew Lockton; 2013-03-09 at 20:42.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post
    Thanks for the code. I thought that " " would test for a space, but it just didn't seem to be working in my macro, or the situations where I was using it, so I will experiment further.

    -Rich

Posting Permissions

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