Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2022, 11:40 PM
laith93 laith93 is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
Remove extra space before a paragraph and return it to previous paragraph
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default Remove extra space before a paragraph and return it to previous paragraph

Hi,


look at this image
11.png

I have many paragraphs like this, so I want to remove extra spaces before the second paragraph in order to return it to the previous paragraph in just one click as shown in Result:
Also, keep in mind that these spaces may be single spaces or many, single return marks or many.

Thanks
Reply With Quote
  #2  
Old 04-18-2022, 01:11 AM
Guessed's Avatar
Guessed Guessed is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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

It is unlikely to be purely spaces. You need to post a sample document or set your options to display all hidden characters before grabbing a screenshot.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-18-2022, 07:00 AM
laith93 laith93 is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
Remove extra space before a paragraph and return it to previous paragraph
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
It is unlikely to be purely spaces. You need to post a sample document or set your options to display all hidden characters before grabbing a screenshot.
Thanks Mr. Andrew
here is the sample file
demo.docx

Also, there may be a return mark or double or more, single space or more, first-line indentation
Reply With Quote
  #4  
Old 04-18-2022, 07:38 PM
Peterson Peterson is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

It's not clear what you want to to with the numerals, but the following code will remove the gaps in the text and remove the empty paragraphs preceding the numerals:
Code:
Sub CleanUpBlankLines() 

' Finds paragraph marks and spaces between broken paragraphs and removes them
' Finds empty paragraphs preceding numerals and removes them, too

    Application.ScreenUpdating = False
    
    Dim oRange As Range
         
    Set oRange = ActiveDocument.Range

    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        
        ' Remove gaps in text:
        .Text = "([!0-9] )([^13 ]{1,})([!0-9])"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
        
        ' Remove spaces between empty paragraphs, in preparation
        ' to remove paragraphs preceding numbers:
        .Text = "(^13)( {1,})(^13)"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
        
        ' Remove empty paragraphs preceding numbers:
        .Text = "([^13]{2,})([0-9])"
        .Replacement.Text = "^13\2"
        .Execute Replace:=wdReplaceAll
    
    End With
    
    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
    End With
    
     Set oRange = Nothing

     Application.ScreenUpdating = True

 End Sub

Last edited by Peterson; 04-19-2022 at 08:29 AM.
Reply With Quote
  #5  
Old 04-19-2022, 12:09 PM
laith93 laith93 is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
Remove extra space before a paragraph and return it to previous paragraph
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Peterson View Post
It's not clear what you want to to with the numerals, but the following code will remove the gaps in the text and remove the empty paragraphs preceding the numerals

Thank you Mr. Peterson
No, Mr. Peterson, I put these numbers in order to know that there are multiple cases of my problem
for example, in case 1, there is a single return mark between the broken paragraph
in case 2, there are multiple return marks between the broken paragraph and so

So the numbers (1,2,3) are just for differentiation of paragraphs (cases) and not included in my text, so I'm sorry for this misunderstanding.
Reply With Quote
  #6  
Old 04-21-2022, 11:48 PM
laith93 laith93 is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
Remove extra space before a paragraph and return it to previous paragraph
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Dear Peterson, after applying your code, I think this portion of your code is not necessary at least according to my case , so I will remove it.

Quote:
Originally Posted by Peterson View Post

Code:
Sub CleanUpBlankLines() 
        
        ' Remove spaces between empty paragraphs, in preparation
        ' to remove paragraphs preceding numbers:
        .Text = "(^13)( {1,})(^13)"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
        
        ' Remove empty paragraphs preceding numbers:
        .Text = "([^13]{2,})([0-9])"
        .Replacement.Text = "^13\2"
        .Execute Replace:=wdReplaceAll
   
 End Sub
Now, this code is ok
Code:
Sub GapRemoving4Paragraph()

' Finds paragraph marks and spaces between broken paragraphs and removes them

    Application.ScreenUpdating = False
    
    Dim oRange As range
         
    Set oRange = ActiveDocument.range

    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        
        ' Remove gaps in text:
        .Text = "([!0-9] )([^13 ]{1,})([!0-9])"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
    
    End With
    
    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
    End With
    
     Set oRange = Nothing

     Application.ScreenUpdating = True

 End Sub
But the only problem is that sometimes the lines of each paragraph are followed by a return mark, and this code is not removing them, as Word considers each line as a separate paragraph, so how to fix it, please?
See attached file
Attached Files
File Type: docx DemoLineParagraph.docx (18.3 KB, 11 views)
Reply With Quote
  #7  
Old 04-25-2022, 06:45 PM
Peterson Peterson is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

In the first sample document, there was a space preceding every paragraph mark in all of the examples, so I wrote the wildcard string to include that space. In addition, the numerals, which were unrelated to the content, let me know where paragraphs ended.

Without the numerals in the first doc, I can't know how to differentiate one paragraph from another, and the macro's purpose is to remove a very common indicator of the end of a paragraph: two consecutive paragraph marks.

Running a modified version of the macro would therefore remove every paragraph mark and add at least one extra space, but you'd end up with a document that consists of just one massive paragraph.

The same would be true for the second document, but there wouldn't be an extra space. (In the second sample document, there are no spaces before or after paragraph marks, which is why the macro isn't working in it.)

You could get around this problem by manually locating the end of every paragraph and typing a short text string as a temporary marker -- I like to use QQ -- run the macro (it needs to be modified first), then replace the markers with paragraph marks.

If the files have spaces at the end of every line because you are copying/pasting from PDFs, then, depending on your PDF app, you should be able to select text in the PDF, right click, and use a "Copy with formatting" feature, which would allow you to paste the text elsewhere without adding paragraph marks at the end of each line. Alternatively, you could open the original PDF into Word, and even though the formatting won't be right, and you wouldn't want to create a file this way, then work on it, you could at least copy text without paragraphs after each line.
Reply With Quote
  #8  
Old 04-27-2022, 08:26 AM
laith93 laith93 is offline Remove extra space before a paragraph and return it to previous paragraph Windows 10 Remove extra space before a paragraph and return it to previous paragraph Office 2019
Competent Performer
Remove extra space before a paragraph and return it to previous paragraph
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Firstly, I want to tell you that all cases you mentioned are come from copying from PDF files (I have been shocked from pdf files)
Quote:
Originally Posted by Peterson View Post
let me know where paragraphs ended.
The paragraphs ended with a full stop (dot), may I not mention the full stop in the demo file because I copied just a portion of paragraphs.

Quote:
Originally Posted by Peterson View Post
use a "Copy with formatting" feature, which would allow you to paste the text elsewhere without adding paragraph marks at the end of each line. .
Wow, It's an amazing feature
After googling, Adobe Acrobat supports this feature,
when you make "Copy with formatting", you must choose "Merge Formatting" to get a single paragraph as you copied it from a pdf file, otherwise, you will get paragraph marks at the end of each line.

Thanks Mr. Peterson
Reply With Quote
Reply

Tags
word 19, word vba, word vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Remove extra space before a paragraph and return it to previous paragraph Delete comma and space after blank merged value. Also remove Previous space and word before blank Alex1s85 Mail Merge 4 01-18-2020 11:30 PM
Remove extra space before a paragraph and return it to previous paragraph Large first character in paragraph causing extra line space dustnik Word 11 09-04-2019 12:47 AM
Add a heading for each paragraph depending on the previous paragraphs numbering herman Word 4 07-15-2019 09:48 PM
Remove extra space before a paragraph and return it to previous paragraph Word thinks 1 sentence is 2 paragraphs and won't let me delete the extra paragraph symbol jrasicmark Word 1 12-05-2014 01:50 AM

Other Forums: Access Forums

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