Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 12-04-2024, 12:56 PM
genja genja is offline Windows 11 Office 2021
Novice
 
Join Date: Aug 2024
Posts: 3
genja is on a distinguished road
Default

Thanks Greg, that put me in the right direction. I thought that I can target the paragraph within the cc specifically but now I just iterate through the entire document.

My goal is to find all empty paragraphs in documents with 3000 (or sometimes more) paragraphs that do not contain necessary breaks and delete them. Tables are excluded by design. There is some redundancy in the code regarding the checking for the breaks which I was too lazy to remove. I am fairly new to VBA, so, if anyone has a suggestion for improvement, please share your thoughts.

Code:
Sub delete_empty_paras()
 Dim doc As Document, para As Paragraph, paraNext As Paragraph, paraRng As Range, _
 paraLen As Integer, delCount As Integer, paraCount_pre As Integer, paraCount_post As Integer
     StartTime = Timer
    Set doc = ActiveDocument: Set para = doc.Paragraphs.First
    Do While Not para Is Nothing
        Set paraNext = para.Next
        If para.Range.Tables.count > 0 Then GoTo Skip
        Set paraRng = para.Range: paraLen = Len(para.Range.text)
        If paraRng.text = ChrW(12) Then GoTo Skip:If paraRng.text = "^m" Then GoTo Skip
        If paraLen <= 2 Then
            If Not ContainsBreaks(paraRng) And Not ContainsShapesOrImages(paraRng) Then
                paraCount_pre = doc.Paragraphs.count
                'paraRng.Select 'debugging
                paraRng.Delete: paraCount_post = doc.Paragraphs.count: del = IIf(paraCount_post <> paraCount_pre, 1, 0)
                delCount = delCount + del
            End If
        End If
Skip:
        Set para = paraNext
    Loop
     Debug.Print "Paragraphs deleted: " & delCount & "  |  Execution time: " &  _
                  Round(Timer - StartTime, 2) & " s  |  " & Format((Timer - StartTime) / 60, "0.00") & " min"
   End Sub
Code:
Function ContainsShapesOrImages(rng As Range) As Boolean
    Dim iLshp As InlineShape, shp As shape
    Dim found As Boolean
    found = False
    For Each iLshp In rng.InlineShapes 'check for InlineShapes
        found = True: Exit For
    Next iLshp
    If Not found Then 'check for regular shapes. do I even need this?
        For Each shp In rng.ShapeRange
            found = True: Exit For
        Next shp
    End If
    ContainsShapesOrImages = found
End Function
Code:
Function ContainsBreaks(rng As Range) As Boolean
     Dim searchText As Variant, found As Boolean, i As Integer
    searchText = Array("^b", "^m", "^12", "^n") 'checks for specific breaks
    found = False
    With rng.Find
        .ClearFormatting: .Forward = True: .Wrap = wdFindStop: .MatchWildcards = False
        For i = LBound(searchText) To UBound(searchText)
            .text = searchText(i)
            If .Execute Then: found = True: Exit For 'exit when found
        Next i
    End With
    ContainsBreaks = found
 End Function
Code:
 Function StopTimer(StartTime As Double) As Double
    Dim elapsedTime As Double
    elapsedTime = Timer - StartTime
    StopTimer = elapsedTime
End Function
Reply With Quote
 

Tags
content control, delete empty, paragraphs



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove extra space before a paragraph and return it to previous paragraph laith93 Word VBA 7 04-27-2022 08:26 AM
a macro to replace paragraph mark with a space applies effect on paragraph marks after the selection drrr Word VBA 2 08-24-2021 03:05 AM
Inserting text from a Userform into a Field in a paragraph in a paragraph in a word document storemaz Word VBA 1 03-13-2020 08:11 AM
Continuous Paragraph across two columns vs Parallel Column Paragraph Pinesh Word 2 03-09-2018 04:24 PM
Cross-reference to paragraph not updating when paragraph moves windhoek2010 Word 1 09-15-2017 08:30 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:56 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