Strip leading tabs/spaces while retaining indent

D

David Turner

I still get quite a lot of documents formatted typewriter style with the
space bar and tabs being used to left indent text.

I know leading spaces and tabs can be stripped by selecting the text, center
justifying the selection and then left justifying the selection, but this
also removes the indent.

I'd thus like to be able to remove a space or tab and adjust the left indent
accordingly so the document "looks" the same.

This macro seems to go half way there but I can seem to find a way to move
the indent left by a single space. ParagraphFormat.IndentCharWidth 1 seems to
left indent to the next tab stop rather than just by one character width.

Sub StripTabsAndSpaces()
Dim oPRg As Paragraph
Dim DoTab As Long
Dim i As Long

i = 0
For Each oPRg In ActiveDocument.Paragraphs
While oPRg.Range.Characters.First = vbTab
oPRg.Range.Characters.First = ""
DoTab = (i + 1)
oPRg.Range.ParagraphFormat.TabIndent (DoTab)
Wend
While oPRg.Range.Characters.First = " "
oPRg.Range.Characters.First = ""
oPRg.Range.ParagraphFormat.IndentCharWidth 1
Wend
Next
End Sub

Any help would be greatly appreciated.
Regards,
David Turner
 
D

Doug Robbins - Word MVP

Why don't you apply a Style after removing the tabs/spaces

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

David Turner

Doug Robbins - Word MVP said:
Why don't you apply a Style after removing the tabs/spaces

Yes, that would probably be best although styles imply some sort of logical
structure and the sort of typewriter style documents that I receive usually
don't have much!
My question was really how to determine the position of an indent set with
the space bar. I guess the code below would do it although I'm not sure about
the exact width of a "space" in points. Presumably it would depend on the
font size and/or name?

Sub RemLeadTabsAndSpaces()

Dim oPRg As Paragraph
Dim DoTab As Long
Dim i As Long
Dim iPointsIndented As Integer

i = 0
For Each oPRg In ActiveDocument.Paragraphs
'Read current paragraph indent in points
iPointsIndented = oPRg.LeftIndent
'MsgBox ("current paragraph indented " & iPointsIndented & " points")
While oPRg.Range.Characters.First = vbTab
oPRg.Range.Characters.First = ""
DoTab = (i + 1)
oPRg.Range.ParagraphFormat.TabIndent (DoTab)
Wend
While oPRg.Range.Characters.First = " "
oPRg.Range.Characters.First = ""
iPointsIndented = iPointsIndented + 2.83
oPRg.LeftIndent = iPointsIndented
Wend
Next
End Sub
 

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