Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-04-2019, 08:38 PM
afif afif is offline Macro to remove table captions leaves empty space Windows 7 64bit Macro to remove table captions leaves empty space Office 2010
Novice
Macro to remove table captions leaves empty space
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default Macro to remove table captions leaves empty space


I tried to run the macro to remove captions in all tables in a document.

Code:
Sub delTableCaptionAll()
    
    
    Dim ctr As Integer
    Dim txt As String
    Dim Check As Boolean
    Dim para As Paragraph
    ctr = 0
    
    
    
    For Each para In ActiveDocument.Paragraphs
        txt = para.Range.Text
        Debug.Print txt
        
        Check = InStr(txt, "Table")
        Debug.Print Check
        
        If Check = True And para.Range.Style = "Caption" Then
            para.Range.Delete
        End If
        ctr = ctr + 1
        Debug.Print ctr
    Next
End Sub
The problem is it doesn't remove the line (image attached) after the caption is deleted. How can I remove the space after the caption is deleted ?
Thanks
Attached Images
File Type: png space.png (1.3 KB, 10 views)
Reply With Quote
  #2  
Old 08-04-2019, 10:30 PM
Guessed's Avatar
Guessed Guessed is offline Macro to remove table captions leaves empty space Windows 10 Macro to remove table captions leaves empty space Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

You will need a second bite at it if the caption is followed by a table since the paragraph mark remains if the paragraph contains any content.
Code:
Sub delTableCaptionAll()
  Dim ctr As Integer, aRng As Range
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "Table"
    .ClearFormatting
    .Style = "Caption"
    .Forward = True
    Do While .Execute
      aRng.Paragraphs(1).Range.Delete
      If aRng.Paragraphs(1).Next.Range.Information(wdWithInTable) Then
        aRng.Delete
      End If
      ctr = ctr + 1
    Loop
    Debug.Print ctr
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-05-2019, 12:01 AM
afif afif is offline Macro to remove table captions leaves empty space Windows 7 64bit Macro to remove table captions leaves empty space Office 2010
Novice
Macro to remove table captions leaves empty space
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default

Thanks. That works fine. But the problem is I insert the caption using macro. For testing purpose. I insert it multiple times.
When I try to delete them, the lines of removed captions are still there.
I try to use the following code to check whether the previous paragraph is empty. If it is empty, then remove the line. But it returns false.

Code:
If aRng.Paragraphs(1).Previous.Range.Text = "" Then
        aRng.Delete
End If
Attached Images
File Type: png line2.png (2.2 KB, 10 views)
Reply With Quote
  #4  
Old 08-05-2019, 01:12 AM
Guessed's Avatar
Guessed Guessed is offline Macro to remove table captions leaves empty space Windows 10 Macro to remove table captions leaves empty space Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

A paragraph range always includes a paragraph mark so Range.Text is going to give you a vbCr value. Try looking for aRng.Paragraphs(1).Previous.Range.Text = vbCr
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-05-2019, 01:14 AM
Guessed's Avatar
Guessed Guessed is offline Macro to remove table captions leaves empty space Windows 10 Macro to remove table captions leaves empty space Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

And the code I provided earlier used the same double test as yours did. It is looking for a style where the paragraph also contains the word 'Table'. Those empty paragraphs fail on the second test.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 08-05-2019, 01:34 AM
afif afif is offline Macro to remove table captions leaves empty space Windows 7 64bit Macro to remove table captions leaves empty space Office 2010
Novice
Macro to remove table captions leaves empty space
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default

Thanks Lockton for the reply, that removes 1 blank line above. But what if I try to remove several blank lines from multiple caption insertions in one table ?

I try to add the code below :
Code:
'loop through all blank lines above the removed line.
Do While aRng.Paragraphs(1).Previous.Range.Text = vbCr
        
        aRng.Delete
Loop
But it doesn't remove all the blank lines
Reply With Quote
  #7  
Old 08-05-2019, 09:24 PM
Guessed's Avatar
Guessed Guessed is offline Macro to remove table captions leaves empty space Windows 10 Macro to remove table captions leaves empty space Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

Remove any instance of empty paragraphs in blocks of text by searching for ^p^p and replacing with ^p
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove space between header and first bullet in Outlook email created by Excel macro kevinbradley57 Excel Programming 1 03-27-2019 08:39 PM
Macro to remove table captions leaves empty space How to remove almost a full page of blank space in between Word table rows garrisonsdad Word Tables 4 10-18-2018 09:09 PM
Macro to remove table captions leaves empty space Can't get rid of empty white space in table wtfword Word Tables 2 01-24-2018 02:47 AM
Macro to remove table captions leaves empty space Remove consecutive empty paragraphs within existing macro kevinbradley57 Word VBA 5 10-11-2017 05:04 PM
Macro to remove table captions leaves empty space Remove space after table in header melliejane Word 1 03-05-2012 04:09 PM

Other Forums: Access Forums

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


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