Word VBA - How to skip whitespace?

Posted on 2011-05-12
Last Modified: 2012-05-11
I need a VBA script to skip white space to help parse lines of text.  The Ctrl-right arrow in Word skips white space, but it also considers "_" a word break.  I'm looking for something that will skip real white space, e.g., tabs and spaces.

I assume VBA can tell me what character the insert point is on, but I can't find it.  That alone would help.
Question by:exoson
    LVL 10

    Expert Comment

    Is this helpful at all?

    Public Sub FindWhiteSpace()

    Dim wordEnt As Object
    For Each wordEnt In ActiveDocument.Characters
        Select Case True
        Case wordEnt = vbTab
            MsgBox "I am a tab"
        Case wordEnt.Text = " "
            MsgBox "I am a space"
        End Select
    Next wordEnt

    End Sub

    Author Comment

    I don't think that is it, but thanks.  I found a sample that I have abused into what I need, but I suspect there is a better way.
    I select a line of text in Word.

    Public Function isWhitespace(char As Byte) As Boolean
        Select Case char
            Case 9, 32:
                isWhitespace = True
        End Select
    End Function

        For i = 1 To Len(Selection.text)
            If (isWhitespace(Asc(Mid$(Selection.text, i, 1)))) Then
                Selection.Start = Selection.Start + 1
                Exit For
            End If
        Next ' i

    This leaves me at the start of whitespace.  I use the opposite to find the next word.


    Author Comment

    The basic task is to convert a script to text in Word.  Script sample:
    %%CALL      FSDev_Close         %%NULL
    '%%CALL      FSDev_Close        Foo_FSDev_funca#1;  \
                                                        Foo_FSDev_func5#1;                    \
                                                        Foo_FSDev_funcd#2;  \
                                                        Foo_FSDev_funce#                1;

    Converts to:

    In the first case the %%NULL indicates no action.
    In the second case the leading Foo_ has been trimmed and the trailing # and all remaining chars on the line are trimmed.

    LVL 4

    Expert Comment

    Regular Expressions are an excellent way to parse text-based files.  Although it may seem a bit complex at first, once you've looked at a few examples, you'll be asking yourself how did you manage without it for so long.

    Here's a good place to look at how it works:
    Microsoft Beefs Up VBScript with Regular Expressions
    LVL 44

    Accepted Solution

    I did this with Find methods using wildcards.  It produces the parsed strings that you posted above in the Immediate window

    Option Explicit
    Public Sub ParseIt()
      Dim rngFind As Range
      Dim strFind As String
      Set rngFind = ActiveDocument.Content
      rngFind.Find.Execute "Foo_*[#]", MatchWildcards:=True
      Do While rngFind.Find.Found = True
        strFind = rngFind.Text
        strFind = Mid$(strFind, 5)
        strFind = Left$(strFind, Len(strFind) - 1)
        Debug.Print strFind
        rngFind.Find.Execute "Foo_*[#]", MatchWildcards:=True
    End Sub

    Open in new window


    Author Comment

    Well that certainly makes a list quick and clean.  Showing my inexperience with VBA, I presume I replace the Debug.Print strFind with code to write the strFind into the document?

    Forgive me if this is a violation of protocol here, but do you accept contracts for this stuff?  I don't have a strong need to learn VBA, just to get the task done.

    LVL 44

    Expert Comment


    Yes.  The code produces your desired parsed text in the strFind variable.  In place of the Debug.Print statement, you would do whatever you need to do with that data.

    There are many experts that have Hire Me buttons on their profiles, including me.  You are welcome to click on these buttons to learn more.
    LVL 38

    Expert Comment

    by:Jim P.
    The basic solution was posted at the accepted answer.

    The Asker and Expert did more with a private off-site contract.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
    I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now