![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Code:
Sub Remove_Trailing_spaces()
Dim i As Integer, rng As Range, d As Integer, e As Integer, para As Paragraph
For e = 1 To ActiveDocument.Paragraphs.Count
Set para = Selection.Paragraphs(1)
'On Error Resume Next
With para.Range
Select Case para.Range.words(1).Text
Case "Now", "So", "Again", "Also", "Given", "Here", "Then", "Let"
Case "or", "Or", "i", "ii", "iii", "iv", "v", "vi", "vii", "vii", "a", "b", "c", "d", "e", "f", "g", "&", " "
d = para.Range.words(1).Characters.Count
If .Characters(d).Next.Text = "." Or .Characters(d).Next.Text = ")" Or .Characters(d).Next.Text = "," Then
d = d + 2
End If
If .words(2).Text <> "," Then
For i = d To .Characters.Count
If .Characters(d).Text = " " Or .Characters(i).Text = Chr(9) Then
.Characters(i).Delete
If i = 10 Then Exit For
Else
Exit For
End If
Next
Else
For i = d To .Characters.Count
If .Characters(i).Text = " " Or .Characters(i).Text = Chr(9) Then
.Characters(i).Delete
i = d
If i = 10 Then Exit For
Else
.Characters(d).InsertAfter vbTab
Exit For
End If
Next
End If
Case Chr(40), Chr(38)
For i = d To .Characters.Count
If .Characters(i).Text = " " Or .Characters(i).Text = Chr(9) Then
.Characters(i).Delete
If i = 10 Then Exit For
Else
Exit For
End If
Next
.Characters(1).InsertAfter vbTab
Case Else
If .words(1) <> "vbtab" Then
.words.First.InsertBefore vbTab
End If
End Select
End With
Next
End Sub
Any other feedback on this code will be welcomed. Last edited by Bikram; 06-03-2022 at 05:57 AM. |
|
#2
|
||||
|
||||
|
Can you explain exactly what you want the code to do?
As a start, the code you have supplied only works on the first selected paragraph. If you want the code to work on every paragraph and it involves deleting stuff then you should work backwards from the end of the document. The initial lines would then be... Code:
For e = ActiveDocument.Paragraphs.Count to 1 step -1 Set para = ActiveDocument.Paragraphs(e)
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
How to Remove trailing spaces AND line breaks in a block of text
|
JulianS96 | Word | 4 | 02-04-2020 04:20 AM |
Deleting text but keeping adjacent spaces
|
Alsadius | Word VBA | 6 | 03-04-2019 02:50 PM |
Removing trailing spaces from footnotes
|
risomd | Word VBA | 11 | 08-30-2018 06:00 PM |
How to remove trailing & leading spaces in a cell?
|
LearnerExcel | Excel Programming | 8 | 02-04-2018 08:22 PM |
Mail Merge - Trailing Spaces
|
osucjb | Mail Merge | 5 | 10-28-2016 12:03 AM |