Delete any leading (redundant) spaces after tab of built-in-headingstyles

A

andreas

Dear Experts,
I got a long word-file where the user has inserted spaces (1 to n)
after the tab of the built-in heading styles (featuring hanging
indents) and before the text of the headings. Example

1.1Tab(HangingIndent)RedundantLeadingSpacesHeadingText

But it should be:
1.1Tab(HangingIndent)HeadingText

So how can I programmatically (VBA) drop these redundant leading
spaces?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
G

Greg Maxey

Andreas,

You might have to change or add to the Cases of "Heading" styles but this
might work.

Sub ScratchMacro()
Dim oPar As Word.Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Paragraphs
Select Case oPar.Style
Case "Heading 1", "Heading 2"
Set oRng = oPar.Range
With oRng.Find
.Text = "(^t)( )@([! ])"
.Replacement.Text = "\1\3"
.MatchWildcards = True
.Execute Replace:=wdReplaceOne
End With
Case Else
'Do nothing
End Select
Next oPar
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~~~~~~~~~~~~
 
D

Doug Robbins - Word MVP

You can do this using Edit>Replace. In the Find and Replace dialog, click
on the More button and then check the "Use wildcards" box and in the Find
what control, enter

(^t)[ ]{1,}

and in the Replace with control, enter

\1

Then click on Replace All

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

andreas

You can do this using Edit>Replace.  In the Find and Replace dialog, click
on the More button and then check the "Use wildcards" box and in the Find
what control, enter

(^t)[ ]{1,}

and in the Replace with control, enter

\1

Then click on Replace All

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




Dear Experts,
I got a long word-file where the user has inserted spaces (1 to n)
after the tab of the built-in heading styles (featuring hanging
indents) and before the text of the headings. Example

But it should be:
1.1Tab(HangingIndent)HeadingText
So how can I programmatically (VBA) drop these redundant leading
spaces?
Help is much appreciated. Thank you very much in advance.
Regards, Andreas- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,

thanks for your quick help. I am getting an error message, telling me
that this expression is not working (on the search and replace dialog
field). I altered the expression to
(^t) {1,} and replaced it with ^t . Now it works but it does not find
the tabs followed by spaces on the built-in headings (heading 1,
heading 2, heading 3). May be it cannot find tabs before the built-in
headings since the tab ist part of the underlying field of the
heading. I hope I made myself clear.

Regards, Andreas
 
A

andreas

Andreas,

You might have to change or add to the Cases of "Heading" styles but this
might work.

Sub ScratchMacro()
Dim oPar As Word.Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Paragraphs
  Select Case oPar.Style
    Case "Heading 1", "Heading 2"
      Set oRng = oPar.Range
      With oRng.Find
        .Text = "(^t)( )@([! ])"
        .Replacement.Text = "\1\3"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceOne
      End With
    Case Else
      'Do nothing
  End Select
Next oPar
End Sub

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey -  Word MVP

My web sitehttp://gregmaxey.mvps.org
Word MVP web sitehttp://word.mvps.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~




Dear Experts,
I got a long word-file where the user has inserted spaces (1 to n)
after the tab of the built-in heading styles (featuring hanging
indents) and before the text of the headings. Example

But it should be:
1.1Tab(HangingIndent)HeadingText
So how can I programmatically (VBA) drop these redundant leading
spaces?
Help is much appreciated. Thank you very much in advance.
Regards, Andreas- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Dear Greg,

thanks for the quick answer. Although I altered it slightly as you
suggested to include my specific heading style, it regrettably did not
work. As I stated below in response to Doug's suggestion, I guess the
tabs of the built-in heading styles cannot be accessed the way you
suggest. I hope I could make myself clear. Thank you for your help.
Regards, Andreas
 
G

Graham Mayor

thanks for your quick help. I am getting an error message, telling me
that this expression is not working (on the search and replace dialog
field). I altered the expression to
(^t) {1,} and replaced it with ^t . Now it works but it does not find
the tabs followed by spaces on the built-in headings (heading 1,
heading 2, heading 3). May be it cannot find tabs before the built-in
headings since the tab ist part of the underlying field of the
heading. I hope I made myself clear.

Regards, Andreas

My guess is that you have the regional settings in Windows to something
other than English. eg if you have the regional settings set to Germany, the
comma in {1,} will throw an error - use a semi colon {1;} instead.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

andreas

My guess is that you have the regional settings in Windows to something
other than English. eg if you have the regional settings set to Germany, the
comma in {1,} will throw an error - use a semi colon {1;} instead.

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

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Graham,

thanks for the tip, but I did consider this already.
Regards, Andreas
 
H

Helmut Weber

Hi Andreas,

As leading spaces are no good anyway:

Sub Testxyz()
Dim oPRg As Paragraph
For Each oPRg In ActiveDocument.Paragraphs
While oPRg.Range.Characters.First = " "
oPRg.Range.Characters.First = ""
Wend
Next
End Sub

I am assuming, that you know how to add a check,
if required, if the paragraph in question is a heading paragraph.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi Doug,

I think, Andreas has automatically numbered headings.

--

Gruß

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi Andreas,

some more clarification:
May be it cannot find tabs before the built-in headings
since the tab ist part of the underlying field of the heading.

You are on the right track.

I don't know much about fields,
but there is no tab in the liststring nor at the paragraph's start.

MsgBox
Len(ActiveDocument.ListParagraphs(1).Range.ListFormat.ListString)
for e.g.
1)<tab>text, text, text...
The Liststring has a length of 2.

There is some algorithm,
that makes Word display a tab.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

andreas

Hi Andreas,

As leading spaces are no good anyway:

Sub Testxyz()
Dim oPRg As Paragraph
For Each oPRg In ActiveDocument.Paragraphs
   While oPRg.Range.Characters.First = " "
      oPRg.Range.Characters.First = ""
   Wend
Next
End Sub

I am assuming, that you know how to add a check,
if required, if the paragraph in question is a heading paragraph.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Hi Helmut,

your code did the trick. Thank you very much. Your code is really good
since - as you mentioned - it will delete any leading spaces on any
paragraph. And this should be the case throughout the document. Again,
thank you very much. Regards, Andreas
 

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