?
Solved

Strip Leading and Trailing spaces - Vba

Posted on 2003-11-25
6
Medium Priority
?
1,498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 9

Accepted Solution

by:
gilbar earned 300 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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 …
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 …
Suggested Courses

764 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