Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old Yesterday, 10:14 AM
RobiNew RobiNew is offline Macro to delete ending period at the end of a single paragraph Windows 11 Macro to delete ending period at the end of a single paragraph Office 2016
Competent Performer
Macro to delete ending period at the end of a single paragraph
 
Join Date: Sep 2023
Posts: 229
RobiNew is on a distinguished road
Default Macro to delete ending period at the end of a single paragraph

Hi! I use this macro to delete an ending period at the end of a single paragraph.
Is there a way to delete the period without having to re-insert the paragraph mark?
Thanks!
Code:
Sub DeleteEndingFullStop()
    Dim para As Paragraph
    Dim txt As String
    Set para = Selection.Paragraphs(1)
    txt = para.Range.Text
    ' Check if paragraph ends with a period before the paragraph mark
    ' check the character before vbCr (ASCII 13)
    If Len(txt) > 1 Then
        If Mid$(txt, Len(txt) - 1, 1) = "." Then
            ' Remove the ending period
            para.Range.Text = Left$(txt, Len(txt) - 2) & vbCr
        End If
    End If
End Sub


Last edited by macropod; Yesterday at 01:50 PM. Reason: Correct code tag usage
Reply With Quote
  #2  
Old Yesterday, 01:52 PM
macropod's Avatar
macropod macropod is offline Macro to delete ending period at the end of a single paragraph Windows 10 Macro to delete ending period at the end of a single paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,511
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Simple:
Code:
Sub DeleteEndingFullStop()
With Selection.Paragraphs.First.Range
  Do While .Characters.Last.Previous = "."
    .Characters.Last.Previous = vbNullString
  Loop
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old Yesterday, 03:09 PM
RobiNew RobiNew is offline Macro to delete ending period at the end of a single paragraph Windows 11 Macro to delete ending period at the end of a single paragraph Office 2016
Competent Performer
Macro to delete ending period at the end of a single paragraph
 
Join Date: Sep 2023
Posts: 229
RobiNew is on a distinguished road
Default

Many thanks, Macropod! Perfectly simple.
Reply With Quote
  #4  
Old Today, 03:40 AM
RobiNew RobiNew is offline Macro to delete ending period at the end of a single paragraph Windows 11 Macro to delete ending period at the end of a single paragraph Office 2016
Competent Performer
Macro to delete ending period at the end of a single paragraph
 
Join Date: Sep 2023
Posts: 229
RobiNew is on a distinguished road
Default

Hi Macropod! Could you please correct the following macro? Thanks!
Code:
Sub FormatAndDelete()
'Format left titles
For Each oPar In ActiveDocument.Paragraphs
'Deletes period at end of paragraph (Fails!!)
    With oPar.Range.Characters
    If oPar.Range.Characters.Last.Previous = "." Then oPar.Range.Characters.Last.Previous = vbNullString
    End With
    If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 _
         And oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _
         And Not oPar.Range Like "*[0-9]*" Then
      oPar.Range.Style = "LeftTitle"
    End If
Next oPar
End Sub

Last edited by macropod; Today at 05:07 AM. Reason: Corrected code tags
Reply With Quote
  #5  
Old Today, 05:18 AM
macropod's Avatar
macropod macropod is offline Macro to delete ending period at the end of a single paragraph Windows 10 Macro to delete ending period at the end of a single paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,511
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Code:
Sub FormatAndDelete()
'Format left titles
Application.ScreenUpdating = False
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
  With oPar.Range
    If .Characters.Last.Previous = "." Then .Characters.Last.Previous = vbNullString
    If .ComputeStatistics(wdStatisticLines) = 1 Then
      If .ParagraphFormat.Alignment = wdAlignParagraphLeft Then
         If Not .Text Like "*[0-9]*" Then .Style = "LeftTitle"
      End If
    End If
  End With
Next oPar
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old Today, 07:27 AM
RobiNew RobiNew is offline Macro to delete ending period at the end of a single paragraph Windows 11 Macro to delete ending period at the end of a single paragraph Office 2016
Competent Performer
Macro to delete ending period at the end of a single paragraph
 
Join Date: Sep 2023
Posts: 229
RobiNew is on a distinguished road
Default

Thanks, Macropod! But I posted the wrong version. Please check this one with regard to period deletion.
Code:
Sub FormatAndDeleteNew()
'Format left titles
For Each oPar In ActiveDocument.Paragraphs
    If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 _
         And oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _
         And Not oPar.Range Like "*[0-9]*" Then
'If above conditions are true, then delete possible period and apply style
    If oPar.Range.Characters.Last.Previous = "." Then oPar.Range.Characters.Last.Previous = vbNullString
      oPar.Range.Style = "LeftTitle"
    End If
Next oPar
End Sub

Last edited by macropod; Today at 01:13 PM. Reason: Added code tags
Reply With Quote
  #7  
Old Today, 01:19 PM
macropod's Avatar
macropod macropod is offline Macro to delete ending period at the end of a single paragraph Windows 10 Macro to delete ending period at the end of a single paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,511
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

C'mon, that's a trivial change:
Code:
Sub FormatAndDelete()
'Format left titles
Application.ScreenUpdating = False
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
  With oPar.Range
    If .ComputeStatistics(wdStatisticLines) = 1 Then
      If .ParagraphFormat.Alignment = wdAlignParagraphLeft Then
        If Not .Text Like "*[0-9]*" Then
          If .Characters.Last.Previous = "." Then .Characters.Last.Previous = vbNullString
          .Style = "LeftTitle"
        End If
      End If
    End If
  End With
Next oPar
Application.ScreenUpdating = True
End Sub
PS: when posting code, please post it between the code tags, i.e.
HTML Code:
[CODE]Your code goes here[/CODE]
not between pairs of code tags as you've been doing.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to delete ending period at the end of a single paragraph Macro to delete table rows based on the absence of a single specific keyword JellehFishh Word VBA 2 06-27-2019 08:23 AM
Highlighting applied to range ending in a paragraph continues to apply to text added after Peterson Word VBA 2 10-08-2018 02:50 PM
Delete starting and ending character of a line Ktulu Word VBA 2 04-26-2018 09:59 AM
Macro to delete ending period at the end of a single paragraph how do you formulate the date by pay period ending? crussell Excel 8 02-23-2016 07:38 AM
Paragraph ending in following page tadlomc Word 1 01-18-2012 05:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:48 PM.


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