Regular Expression Pattern

A

Ansgar Bockstiegel

Hello,

with a VBA-macro I'd like to find and delete whitespace at the beginning
of each line. I was'nt successfull with the Find-Object and gave the
RegExp-Object a try. But still I didn't find out how to describe that
I'm only searching for whitespace at the beginning of the line.
The pattern I already tried was "^[\s]*".
Any suggests?

Ansgar
 
J

Jezebel

Using ordinary Find and Replace, look for paragraph mark followed by white
space, and replace with paragraph mark. That will deal with all lines other
than the first.
 
A

Ansgar Bockstiegel

The fact, that your solution doesn't fit the first line doesn't matter,
but there are two points that I don't understand:
1. What does "ordinary Find and Replace" mean?
2. How can I look for the paragraph mark?

Thanks.
 
D

Doug Robbins - Word MVP

In case there is more than one space, it might be better to use a Wildcard
replace with
^13[ ]{1,}

in the Find what control, and

^p

in the replace with control.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

If the white space begins a paragraph then select all CTRL+A then (centre)
CTRL+E followed by (left align) CTRL+L will remove it - including from the
first line.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

The fact, that your solution doesn't fit the first line

none of the solutions fits the first line in a doc,
in fact, I don't think this is possible with find
and replace, without possible unwanted sideeffects.

Add this:

With ActiveDocument.Range
While .Characters.First = " "
.Characters.First.Delete
Wend
End With

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003 (US-version)
"red.sys" & Chr$(64) & "t-online.de"
 
A

Ansgar Bockstiegel

Ansgar said:
Hello,

with a VBA-macro I'd like to find and delete whitespace at the beginning
of each line. I was'nt successfull with the Find-Object and gave the
RegExp-Object a try. But still I didn't find out how to describe that
I'm only searching for whitespace at the beginning of the line.
The pattern I already tried was "^[\s]*".
Any suggests?

Ansgar
Thank you for your help.
I tried the pattern Jezebel suggested and it worked fine. The method
with centering and left-aligning the text works only, if you do it
manual bot does not in a macro.

Ansgar
 
K

Klaus Linke

The method with centering and left-aligning the text
works only, if you do it manual bot does not in a macro.


Hi Ansgar,

The macro code for that would be

ActiveDocument.Content.Select
WordBasic.CenterPara
WordBasic.LeftPara

But it's a bit dangerous because it left-aligns all your paragraphs.
It would be safer/better to set all paragraphs to the alignment defined in
the style, but that would need more code, and would be slower than Jezebel's
Find/Replace.

Still -- it's a nice trick that I use occasionally on a bunch of paragraphs,
or table cells, in the user interface.

Greetings,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top