Solved

Strip Leading and Trailing spaces - Vba

Posted on 2003-11-25
6
1,416 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
ID: 9820717
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
ID: 9820770
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
ID: 9821135
gilbar,

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

Thanks
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 9

Accepted Solution

by:
gilbar earned 75 total points
ID: 9821297
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
ID: 9821309
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
ID: 9821510
Works great, gilbar. Thanks for the vba code. I will work on the trailing spaces.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

821 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