Solved

Strip Leading and Trailing spaces - Vba

Posted on 2003-11-25
6
1,372 Views
Last Modified: 2012-06-21
Hello,

I have some lines in my word document, which when viewed with bookmarks turned on, has a string of continuous leading or trailing spaces.(which I believe is created by pressing continuous spacebars).

One example of a line is:
==================================

                                           provided at the end.

==================================
The text  and the paragraphs in the document are aligned properly, pagemargins are fine. The only thing that annoys is these leading or trailing spaces. This annoys because we use these rtfs to another application and there these lines mess up.

Therefore I was wondering is there a macro or a vba code that can eliminate these leading or trailing spaces, and the text intend or alignment should not change by removing them. I would very happy if there is a way to convert these spaces to tabstops or so.

Anyway, a vba code to strip the leading and trailing spaces without having any impact on the format of the document, would help me greatly.

Thanks
0
Comment
Question by:WannabieWordie
  • 4
  • 2
6 Comments
 
LVL 9

Expert Comment

by:gilbar
Comment Utility
this is what i've got so far:
Sub ta()

Dim X As Long
Dim y As Long
Dim Blan As Boolean
Blan = True
X = 0
Selection.HomeKey unit:=wdStory
Do While Blan = True
    Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
    If Selection = " " Then
        X = X + 1
        Blan = True
        Selection.Collapse direction:=wdCollapseEnd
    Else
        If X <> 0 Then
            Blan = False
            Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdMove
            y = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
            Selection.MoveLeft unit:=wdCharacter, Count:=X, Extend:=wdExtend
            Selection.Delete
            Selection.ParagraphFormat.TabStops.ClearAll
            Selection.ParagraphFormat.TabStops.Add Position:=y
            Selection.TypeText vbTab
        End If
        Selection.EndKey unit:=wdLine, Extend:=wdMove
            Selection.MoveRight unit:=wdCharacter, Extend:=wdMove
        X = 0
        y = 0
    End If
    Selection.ParagraphFormat.TabStops.ClearAll
    Blan = True
Loop



End Sub
0
 
LVL 9

Expert Comment

by:gilbar
Comment Utility
it goes through (looping line by line,counting character by character) until it finds a nonspace, then it measures how far it is to the text boundary, then it removes the spaces, sets tab stop that far and put in a tab. Then it hops to the next line.
I tried it, and it seemed to work on my test case but I haven't tested much.  Don't know if there's a better way (I never worried about spaces before).  It doesn't do trailing spaces, but the same concept of counting should work there ( just won't need to have a tab). I'll look at this some more later, but that's what I've got for you now.
0
 

Author Comment

by:WannabieWordie
Comment Utility
gilbar,

The code seems to be working, but it goes into an infinite loop after executing for the last line...
Any clues.

Thanks
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Accepted Solution

by:
gilbar earned 75 total points
Comment Utility
oops! knew i'd forget something
replace
Selection.MoveRight unit:=wdCharacter, Extend:=wdMove  
with
If Selection.MoveRight(unit:=wdCharacter, Extend:=wdMove) = 0 Then Exit Do

(that's in the else section )

so  the whole thing should be like
Sub ta()

Dim X As Long
Dim y As Long
Dim Blan As Boolean
Blan = True
X = 0
Selection.HomeKey unit:=wdStory
Do While Blan = True
    Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
    If Selection = " " Then
        X = X + 1
        Blan = True
        Selection.Collapse direction:=wdCollapseEnd
    Else
        If X <> 0 Then
            Blan = False
            Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdMove
            y = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
            Selection.MoveLeft unit:=wdCharacter, Count:=X, Extend:=wdExtend
            Selection.Delete
            Selection.ParagraphFormat.TabStops.ClearAll
            Selection.ParagraphFormat.TabStops.Add Position:=y
            Selection.TypeText vbTab
        End If
        Selection.EndKey unit:=wdLine, Extend:=wdMove
         If Selection.MoveRight(unit:=wdCharacter, Extend:=wdMove) = 0 Then Exit Do
        X = 0
        y = 0
    End If
    Selection.ParagraphFormat.TabStops.ClearAll
    Blan = True
Loop



End Sub
0
 
LVL 9

Expert Comment

by:gilbar
Comment Utility
if it's at the end, the moveright can't move so it returns zero so then it exits the loop.
0
 

Author Comment

by:WannabieWordie
Comment Utility
Works great, gilbar. Thanks for the vba code. I will work on the trailing spaces.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
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 teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

772 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

15 Experts available now in Live!

Get 1:1 Help Now