Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2022, 06:32 AM
Bikram Bikram is offline I have a code in vba to delete trailing spaces but it is deleting others text too. Windows 10 I have a code in vba to delete trailing spaces but it is deleting others text too. Office 2007
Advanced Beginner
I have a code in vba to delete trailing spaces but it is deleting others text too.
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default I have a code in vba to delete trailing spaces but it is deleting others text too.


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
It works fine for some documents and does not work in the same way for others. I have attached the document where I ran the code. Plz, Check it out.
Any other feedback on this code will be welcomed.
Attached Files
File Type: docx sample.docx (13.4 KB, 11 views)

Last edited by Bikram; 06-03-2022 at 05:57 AM.
Reply With Quote
  #2  
Old 06-06-2022, 04:36 PM
Guessed's Avatar
Guessed Guessed is offline I have a code in vba to delete trailing spaces but it is deleting others text too. Windows 10 I have a code in vba to delete trailing spaces but it is deleting others text too. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a code in vba to delete trailing spaces but it is deleting others text too. How to Remove trailing spaces AND line breaks in a block of text JulianS96 Word 4 02-04-2020 04:20 AM
I have a code in vba to delete trailing spaces but it is deleting others text too. Deleting text but keeping adjacent spaces Alsadius Word VBA 6 03-04-2019 02:50 PM
I have a code in vba to delete trailing spaces but it is deleting others text too. Removing trailing spaces from footnotes risomd Word VBA 11 08-30-2018 06:00 PM
I have a code in vba to delete trailing spaces but it is deleting others text too. How to remove trailing & leading spaces in a cell? LearnerExcel Excel Programming 8 02-04-2018 08:22 PM
I have a code in vba to delete trailing spaces but it is deleting others text too. Mail Merge - Trailing Spaces osucjb Mail Merge 5 10-28-2016 12:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:00 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft