Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 10-11-2017, 10:00 AM
kevinbradley57 kevinbradley57 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jul 2017
Posts: 25
kevinbradley57 is on a distinguished road
Default Remove consecutive empty paragraphs within existing macro

The code below removes a particular paragraph and the subsequent table. The paragraph is preceded by an empty paragraph, and the table is followed by an empty paragraph (both empty paragraphs are used as spacers). It works great, but after it runs there are two consecutive empty paragraphs. I need the code to remove one of those empty paragraphs. Help?



Code:
Sub DeletePriorAuditResults()
'
' DeletePriorAuditResults Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Prior Audit Results^p"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Find.Execute
End Sub
Attached Files
File Type: dotm Audit Report Ver D1.6a.dotm (149.3 KB, 2 views)

Last edited by kevinbradley57; 10-11-2017 at 04:53 PM.
Reply With Quote
  #2  
Old 10-11-2017, 01:38 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try:
Code:
Sub DeletePriorAuditResults()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^pPrior Audit Results^p"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .Execute
  End With
  If .Find.Found = True Then
    .MoveEnd wdTable, 1
    .MoveEnd wdParagraph, 1
    .Text = vbNullString
  End If
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 10-11-2017, 02:14 PM
kevinbradley57 kevinbradley57 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jul 2017
Posts: 25
kevinbradley57 is on a distinguished road
Default

Paul,

Your code appears to be a more efficient way to remove the table, but it still leaves two consecutive empty paragraphs. I need it to leave only one.

Kevin
Reply With Quote
  #4  
Old 10-11-2017, 03:23 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

In my testing it removes one paragraph break before your text and one paragraph after the table. Are you sure there is only one empty paragraph either side?
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 10-11-2017, 04:55 PM
kevinbradley57 kevinbradley57 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jul 2017
Posts: 25
kevinbradley57 is on a distinguished road
Default

I've attached the template I'm working with. I see only one empty paragraph on either side of the table. Maybe I'm missing something?
Reply With Quote
  #6  
Old 10-11-2017, 05:04 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

I've run the macro against a document produced from your template and established that no empty paragraphs remain where 'Prior Audit Results' and the table had been! No empty paragraphs remain.

If anything, I'd have thought you wanted to leave one such paragraph - which is consistent with the way the other headings/tables are structured. In that case, omit:
.MoveEnd wdParagraph, 1
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
automatic insert hypens until the end of the row and in empty rows between paragraphs Ivica Word 3 12-28-2015 01:31 PM
making translated paragraphs consecutive user2969 Word 1 01-29-2014 09:00 PM
Delete lots of empty space between paragraphs. FieldTechnician Word 4 10-25-2013 01:14 PM
Remove empty lines at the top of every page dexter30 Word VBA 2 08-05-2013 08:37 PM
How to remove that pesky empty Page widescreenforever Word 1 11-28-2012 02:49 PM


All times are GMT -7. The time now is 08:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft