none
Trimming spaces at the end of a line RRS feed

  • Question

  • Hello,

    I am having issues with how to trim spaces at the end of a line.

    For example:

    Here is the output in Word interface.

    The quick brown fox jumped over the lazy dog.

    I want to trim the sentence to output it in Notepad.

    The quick brown fox.

    So far I have the following code snippet:

    Dim strEnd As String
    Dim iWord As Long
    Dim iRange As Range
    
    Sub TrimAtEndOfASentence()
    '
    ' TrimAtEndOfASentence Macro
    '
    '
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    iWord = 1
    iWordCount = Word.ActiveDocument.Words(iWord)
    
    
    
    Do Until EOF(1)
        Do Until iWord <= iWordCount
            strEnd = Selection.Text
            Print #1, strEnd
            iWord = iWord + 1
        Loop
    Loop
    
    Close
    
    End Sub
    

    So far, I do not have any return output in Notepad.

    Can anyone fix this code snippet?

    Regards,

    JohnDBCTX


    jp

    Sunday, August 6, 2017 4:14 PM

Answers

  • Dim iWord As Long
    Dim iWordCount As Long
    Dim strTrimEndText, strText As String
    Dim clsHiLight As clsHiLightToEnd
    Dim strUnTrimmed, strTrimmed As String
    Dim ToChange As Long
    Dim NName As String
    
    Sub TrimAtEndOfSentence()
    Set clsHiLight = New clsHiLightToEnd
    
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
        clsHiLight.modHighlightToEnd
        strUnTrimmed = Selection.Text
        ToChange = InStr(iWord, strUnTrimmed, Right(strUnTrimmed, 2))
        NName = Left(strUnTrimmed, ToChange - 1)
        Print #1, NName;
    Next
    
    Close
    End Sub
    
    
    Function modHighlightToEnd()
    '
    ' modHighlightToEnd Macro
    '
    '
    Dim lngHiLite As Long
    Dim wrdSelect As Word.Selection
    Dim strX As String
    
    With Selection
        lngHiLite = .EndKey(Unit:=wdLine, Extend:=wdExtend)
        .Copy
    End With
    
    End Function
    
    

    What is read from the Word App interface:

    The quick brown fox jumped over the lazy dog.

    The output at runtime removes the end of the string which is the period at the end of the sentence.

    Output In Notepad:

    The quick brown fox jumped over the lazy dog

    Does that answer my question?

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Wednesday, August 9, 2017 4:45 AM
    Wednesday, August 9, 2017 4:43 AM
  • For your information in regards to those two built-in functions:

    The InStr function returns a required parameter assigned to as a long data type.

    The Left function returns a required parameter assigned to as a string data type.

    Users need to declare a long parameter type just to assign to the InStr function, and to declare a string parameter type to assign to the Left function.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Wednesday, August 9, 2017 5:02 AM
    Wednesday, August 9, 2017 5:01 AM
  • By the way, has any user ever tried the built-in Mid and Mid$ function?

    Dim iWord As Long
    Dim iWordCount As Long
    Dim strText As String
    Dim lngHiLite As Long
    
    Sub TrimAtEndOfSentence()
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
    lngHiLite = Word.Selection.EndKey(Unit:=wdLine, Extend:=wdWord)
    strText = Word.Selection.Text
    
    Print #1, Mid(strText, 1, Len(strText) - 2)
    Print #1, vbNewLine
    Print #1, Mid$(strText, 1, Len(strText) - 2)
    Print #1, vbNewLine
    
    Next
    
    Close #1
    End Sub
    
    

    Both of the Mid and Mid$ return the same output in Notepad when they are returned as string data type.

    The Len function returns the number of characters.

    To explain this further I have added some comments within the following code snippet:

    Dim iWord As Long
    Dim iWordCount As Long
    Dim strText As String
    Dim lngHiLite As Long
    
    Sub TrimAtEndOfSentence()
    'Opens the file for output
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    'Assign the number of sentences
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
    lngHiLite = Word.Selection.EndKey(Unit:=wdLine, Extend:=wdWord)
    'Highlights to the end of a sentence without the need of copy method.
    
    strText = Word.Selection.Text
    'Assign the strText parameter to Word.Selection.Text after it is declared as string data type.
    Print #1, Mid(strText, 1, Len(strText) - 2)
    'Trim at the end of the sentence.
    Print #1, vbNewLine
    'Go to the next line in Notepad.
    Print #1, Mid$(strText, 1, Len(strText) - 2)
    'Trim at the end of the sentence.
    Print #1, vbNewLine
    'Go To the next line in Notepad.
    
    Next
    
    Close
    'Closes the file.
    
    End Sub
    
    
    

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Friday, August 11, 2017 3:16 AM
    Friday, August 11, 2017 3:16 AM

All replies

  • I have revised the code snippet:

    Dim para As Paragraph
    Dim wrd As Range
    Dim iWord As Long
    Dim iWordCount As Long
    
    iWord = 1
    lngCt = 0
    
    Set para = ActiveDocument.Paragraphs(1)
    iWordCount = para.Range.Words.Count
    Do While iWord <= iWordCount
    
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = ""
        End With
        Selection.Find.Execute
        macBold = Selection.Text
        lngCt = lngCt + 1
        Print #1, lngCt; ", " & macBold
        iWord = iWord + 1
    Loop

    What I want to include within the stored parameter, is how to trim the trailing spaces at the end of the sentence.

    Is it any of the following methods?

    Trim$, Trim, Mid, Left, Left$, or Right$?

    Note that all of those built-in methods are returned as string data type.

    Regards,

    JohnDBCTX


    jp

    Sunday, August 6, 2017 6:43 PM
  • Hi JohnDBCTX,

    Have you tried Trim function? Could it work for you? In my test, if the sentence is the end of a paragraph, there will be a Carriage Return character at the end which makes Trim doesn't work. You could try to replace the Carriage Return character and then use the Trim function.

    Set para = ActiveDocument.Paragraphs(1)

    macBold = Selection.Text

    Print #1, Trim(Replace(macBold, Chr(13), " "))

    Best Regards,

    Terry


    Monday, August 7, 2017 6:06 AM
  • Or you could use:

    Trim(Split(macBold, vbCr)(0))

    which will also process content in table cells. Either way, there is no need for two Trim statements.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, August 7, 2017 6:45 AM
  • Hi macropod,

    Thanks for your remind, I have updated the code.

    Best Regards,

    Terry

    Monday, August 7, 2017 6:57 AM
  • Dim iWord As Long
    Dim iWordCount As Long
    Dim strTrimEndText, strText As String
    Dim clsHiLight As clsHiLightToEnd
    Dim strUnTrimmed, strTrimmed As String
    Dim ToChange As Long
    Dim NName As String
    
    Sub TrimAtEndOfSentence()
    Set clsHiLight = New clsHiLightToEnd
    
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
        clsHiLight.modHighlightToEnd
        strUnTrimmed = Selection.Text
        ToChange = InStr(iWord, strUnTrimmed, Right(strUnTrimmed, 2))
        NName = Left(strUnTrimmed, ToChange - 1)
        Print #1, NName;
    Next
    
    Close
    End Sub
    
    
    Function modHighlightToEnd()
    '
    ' modHighlightToEnd Macro
    '
    '
    Dim lngHiLite As Long
    Dim wrdSelect As Word.Selection
    Dim strX As String
    
    With Selection
        lngHiLite = .EndKey(Unit:=wdLine, Extend:=wdExtend)
        .Copy
    End With
    
    End Function
    
    

    What is read from the Word App interface:

    The quick brown fox jumped over the lazy dog.

    The output at runtime removes the end of the string which is the period at the end of the sentence.

    Output In Notepad:

    The quick brown fox jumped over the lazy dog

    Does that answer my question?

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Wednesday, August 9, 2017 4:45 AM
    Wednesday, August 9, 2017 4:43 AM
  • A potentially serious issue you will have using:
    Word.ActiveDocument.Sentences.Count
    is that
    VBA has no idea what a grammatical sentence is. For example, consider the following:
    Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
    For you and me, that would count as one sentence; for VBA it counts as 5 sentences.

    With that proviso, all you really need do if you want to eliminate the last character of each VBA 'sentence' is to point a Range object to the 'sentence', then move the end of the range backwards by one character. For example:

    Sub Demo()
    Dim i As Long, Rng As Range, strOut As String
    With ActiveDocument
      For i = 1 To .Sentences.Count
        Set Rng = .Sentences(i).Range
        With Rng
          .End = .End - 1
          strOut = strOut & Trim(.Text) & vbCr
        End With
      Next
    End With
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    Print #1, strOut
    Close #1
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, August 9, 2017 5:00 AM Code enhancement
    Wednesday, August 9, 2017 4:55 AM
  • For your information in regards to those two built-in functions:

    The InStr function returns a required parameter assigned to as a long data type.

    The Left function returns a required parameter assigned to as a string data type.

    Users need to declare a long parameter type just to assign to the InStr function, and to declare a string parameter type to assign to the Left function.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Wednesday, August 9, 2017 5:02 AM
    Wednesday, August 9, 2017 5:01 AM
  • By the way, has any user ever tried the built-in Mid and Mid$ function?

    Dim iWord As Long
    Dim iWordCount As Long
    Dim strText As String
    Dim lngHiLite As Long
    
    Sub TrimAtEndOfSentence()
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
    lngHiLite = Word.Selection.EndKey(Unit:=wdLine, Extend:=wdWord)
    strText = Word.Selection.Text
    
    Print #1, Mid(strText, 1, Len(strText) - 2)
    Print #1, vbNewLine
    Print #1, Mid$(strText, 1, Len(strText) - 2)
    Print #1, vbNewLine
    
    Next
    
    Close #1
    End Sub
    
    

    Both of the Mid and Mid$ return the same output in Notepad when they are returned as string data type.

    The Len function returns the number of characters.

    To explain this further I have added some comments within the following code snippet:

    Dim iWord As Long
    Dim iWordCount As Long
    Dim strText As String
    Dim lngHiLite As Long
    
    Sub TrimAtEndOfSentence()
    'Opens the file for output
    Open "C:\Users\johnd\Documents\Trim At End Of A Sentence.txt" For Output As #1
    'Assign the number of sentences
    iWordCount = Word.ActiveDocument.Sentences.Count
    
    For iWord = 1 To iWordCount
    lngHiLite = Word.Selection.EndKey(Unit:=wdLine, Extend:=wdWord)
    'Highlights to the end of a sentence without the need of copy method.
    
    strText = Word.Selection.Text
    'Assign the strText parameter to Word.Selection.Text after it is declared as string data type.
    Print #1, Mid(strText, 1, Len(strText) - 2)
    'Trim at the end of the sentence.
    Print #1, vbNewLine
    'Go to the next line in Notepad.
    Print #1, Mid$(strText, 1, Len(strText) - 2)
    'Trim at the end of the sentence.
    Print #1, vbNewLine
    'Go To the next line in Notepad.
    
    Next
    
    Close
    'Closes the file.
    
    End Sub
    
    
    

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Friday, August 11, 2017 3:16 AM
    Friday, August 11, 2017 3:16 AM