Solved

Strip Leading and Trailing spaces - Vba

Posted on 2003-11-25
6
1,445 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

730 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