• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

Word VBA - How to skip whitespace?

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.
0
exoson
Asked:
exoson
1 Solution
 
borgunitCommented:
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
0
 
exosonAuthor Commented:
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
        Else
            Exit For
        End If
    Next ' i

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

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

Converts to:
None.
-or-
FSDev_funca
FSDev_func5
FSDev_funcd
FSDev_funce

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.

Scott
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LedigimateCommented:
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
0
 
aikimarkCommented:
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
  Loop
End Sub

Open in new window

0
 
exosonAuthor Commented:
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.

Scott
0
 
aikimarkCommented:
Scott

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.
0
 
Jim P.Commented:
The basic solution was posted at the accepted answer.

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now