• Word 2010 Macro to Test for Space

    Author
    Topic
    #487899

    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

    Viewing 3 reply threads
    Author
    Replies
    • #1379228

      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.

    • #1379420

      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!

    • #1379488

      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
    • #1379524

      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

    Viewing 3 reply threads
    Reply To: Word 2010 Macro to Test for Space

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: