MS Office Forum / Word / Programming / March 2008
Delete any leading (redundant) spaces after tab of built-in-heading styles
|
|
Thread rating:  |
andreas - 02 Mar 2008 20:43 GMT 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
Greg Maxey - 02 Mar 2008 22:08 GMT 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
 Signature ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dear Experts, > I got a long word-file where the user has inserted spaces (1 to n) [quoted text clipped - 12 lines] > > Regards, Andreas andreas - 03 Mar 2008 11:27 GMT > Andreas, > [quoted text clipped - 46 lines] > > - 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
Doug Robbins - Word MVP - 02 Mar 2008 22:16 GMT 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
 Signature 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) [quoted text clipped - 12 lines] > > Regards, Andreas andreas - 03 Mar 2008 11:19 GMT On 2 Mrz., 23:16, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org> wrote:
> 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 [quoted text clipped - 34 lines] > > - 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
Graham Mayor - 03 Mar 2008 11:48 GMT > 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 [quoted text clipped - 6 lines] > > 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.
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
andreas - 03 Mar 2008 15:26 GMT > > 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 [quoted text clipped - 18 lines] > Word MVP web sitehttp://word.mvps.org > <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham,
thanks for the tip, but I did consider this already. Regards, Andreas
Helmut Weber - 03 Mar 2008 15:39 GMT 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
andreas - 03 Mar 2008 21:02 GMT > Hi Andreas, > [quoted text clipped - 19 lines] > > 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
Helmut Weber - 03 Mar 2008 16:07 GMT 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
Helmut Weber - 03 Mar 2008 15:50 GMT Hi Doug,
I think, Andreas has automatically numbered headings.
--
Gruß
Helmut Weber, MVP WordVBA
Vista Small Business, Office XP
|
|
|