Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Strip Leading and Trailing spaces - Vba

Posted on 2003-11-25
6
Medium Priority
?
1,518 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
[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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

618 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