Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

leading / trailing spaces 3

Status
Not open for further replies.

dcompto

Technical User
Jul 5, 2001
751
US
I frequently strip leading/trailing spaces from text using:

Ctrl+A
Align Center
Align Left


I recorded a macro of these same steps which reads:

Code:
Sub strip()
    Selection.WholeStory
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub
But when executed, all it does is Select All and stops, as if there are no other commands, leaving the text selected.

Does anyone know how to get these actions to work in a macro?
 
Hi,

What does alignment have to do with stripping leading & trailing spaces?

Code:
    Selection.WholeStory
does select all text in the document.

I'm confused. What are you trying to do?

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Hi, Skip,

I would have never guessed alignment would have anything to do with stripping leading & trailing spaces, except I read a tip a long time ago that Word would strip them if you first "center" the text then set the alignment back to "left"--and it works!

I was just trying to implement that little shortcut into a macro--and it doesn't work!
 
Have 100's if not 1000's of log entries such as...
Code:
????????DJS:?1??CarrName:?TON016_PD1T_1????????????????????????????<p>
????????Carrier:?SN?????0?OC3S???0?????????<p>
????????Previous?State:?SYSB<p>
<p> = paragraph marker

My macro converts each 3-line entry into a single-line entry. Everything works in the macro except for the stripping of the leading / trailing spaces.

Even if there is other code that will accomplish it, I would really like to know WHY the "3-step tip" works when done manually, but not when executed in a macro.
 
Sorry, my "space character" converted to a "questionmark character" when I pressed the Submit button...It's been one of those days...
 
Hi dcompto,

From time to time the Macro Recorder is just plain wrong - it's not a perfect world!!

AFAIK, the only way to execute the alignment commands which strip the spaces is via the toolbar icons or the equivalent Word commands (CenterPara, etc.). So, if that's what you've got to do, then that's what you've got to do ..
Code:
[blue]Sub strip()
    Selection.WholeStory
    Application.CommandBars("Formatting").Controls("Center").Execute
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub[/blue]

I guess you could be a litle more specific in tracking down the correct control but that should do the trick for you unless you have a highly tailored (or messed up) environment.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Well I'll be darned!

I can't get it to work either???

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Tony,

Your code worked just great!

I don't understand why "...wdAlignParagraphLeft" works in a macro but "...wdAlignParagraphCenter" doesn't. Especially, since both commands were created by pressing the toolbar icons during Macro Record. As you said:
From time to time the Macro Recorder is just plain wrong
How you knew to use "Application.CommandBars("Formatting").Controls("Center").Execute" is beyond me.

I don't know if I'll ever know enough of even the basics of VBA!

Thanks for your help (and Skip's, too).
 
[sub]To-ny![/sub] [sub]To-ny![/sub] To-ny! To-ny!



Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Hi dcompto,

Neither of them 'works' - it's just the Centre Alignment which removes the spaces, so that's the one you need to treat specially. The left alignment is just putting the stripped text back where it came from so can be done any way you wish.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Why try to re-invent the wheel, down load this add in ( its free)with over 300 tools, one of them is "Delete leading and trailing spaces
 
Thanks, Dsuperc,

I already have ASAP Utilities installed, but my original post on this thread pertained to a Word document. MY APOLOGIES, especially since my only frustration with the Forum is when someone does NOT SPECIFY TO WHICH APPLICATION OF OFFICE their question pertains! LOL!

Thanks, again, for posting the link for ASAP Utilities.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top